【发布时间】:2015-08-06 11:23:43
【问题描述】:
我是 mySQL 的新手。我有多个查询 sql 要组合,我正在使用 php 和 mySQL。如何组合查询?我需要在表格的一行中显示查询的所有结果。
<?php
$query1="SELECT s.staffName, s.staffNo, g.grade, g.gradePosition, g.gradeDepartment
FROM tblstaff s, tblgrade g where s.staffNo=g.staffNo";
$result=mysql_query($query1) or die (mysql_error());
while($row= mysql_fetch_array($result))
{
?>
<td><?php echo $row['staffName']; ?></td> <!--display staff name-->
<td><?php echo $row['staffNo']; ?></td> <!--display staff number-->
<td><?php echo $row['grade']; ?></td> <!--display staff grade-->
<td><?php echo $row['gradePosition']; ?></td> <!--display staff position-->
<td><?php echo $row['gradeDepartment']; ?></td><!--display staff department-->
<tr>
<?php
}
?>
<?php
$query2="select catTechnical, catOtherTechnical, catTechnicalDescription, catOtherTechnicalDescription, catWeightage,
perReqScore
from tblcategory c join tblperformance p on c.catID=p.catID";
$result=mysql_query($query2) or die (mysql_error());
while($row= mysql_fetch_array($result))
{
?>
<td><?php echo $row['catTechnical']; ?></td> <!--display technical category-->
<td><?php echo $row['catTechnicalDescription']; ?></td> <!--display technical description-->
<td><?php echo $row['catOtherTechnicalDescription']; ?></td> <!--display other technical description-->
<td><?php echo $row['catWeightage']; ?></td> <!--display weightage-->
<td><?php echo $row['perReqScore']; ?></td <!--display required score-->
<?php
}
?>
这是我的数据库表。
tblstaff
+---------+-----------+
| staffNo | staffName |
+---------+-----------+
| 1002435 | Fadzlan |
+---------+-----------+
tblgrade
+---------+---------+-------+---------------+-----------------+
| gradeID | staffNo | grade | gradePosition | gradeDepartment |
+---------+---------+-------+---------------+-----------------+
| 1 | 1002435 | E14 | Manager | IB |
+---------+---------+-------+---------------+-----------------+
tblcategory
+-------+--------------+---------------------------+-------------------------+--------------+
| catID | catTechnical | catOtherTechnical | catTechnicalDescription | catWeightage |
+-------+--------------+---------------------------+-------------------------+--------------+
| 18 | Project(18) | Project Coordination(181) | 30 | |
+-------+--------------+---------------------------+-------------------------+--------------+
tbl 性能
+-------+-------+----------+-------------+
| perID | catID | staffNo | perReqScore |
+-------+-------+----------+-------------+
| 1 | 18 | 10028531 | 4 |
+-------+-------+----------+-------------+
这是我当前的代码和数据库表。我需要将查询 1 和查询 2 组合起来,因为我想通过 staffNo 在表的一行中显示结果。我的意思是一个工作人员没有表中的一行。然后其他staffNo会显示到同一张表的新行中。
【问题讨论】:
-
Yana Rina - 您在此处提供的代码只是将字符串分配给 PHP (Ithink) 变量。也展示你是如何执行它的。
-
UNION可以在这里使用,如果您希望合并多个SELECT语句的结果。 -
请发布您的表结构和您想要检索的内容的示例,根据您的描述,您可能需要 UNION 或 JOIN 才能获得您想要的内容。
-
需要加入多少张表?您需要所有表中的所有行还是只需要具有相同 id 的行?
-
我需要加入 4 个表,分别是表 tblstaff、tblgrade、tblcategory 和 tblperformance。我的第三个查询是加入两个表,即表类别和表性能。我需要显示具有相同 id @User_T 的行