如何结合多个mySQL查询?

时间:2015-05-25 02:43:46

标签: php mysql select

我是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 |              |
+-------+--------------+---------------------------+-------------------------+--------------+

tblperformance

+-------+-------+----------+-------------+
| perID | catID | staffNo  | perReqScore |
+-------+-------+----------+-------------+
|     1 |    18 | 10028531 |           4 |
+-------+-------+----------+-------------+

这是我当前的代码和数据库表。我需要组合查询1和查询2,因为我想通过staffNo在表的一行中显示结果。我的意思是表中的一行没有一个staffNo。然后其他staffNo将显示在同一个表的新行中。

2 个答案:

答案 0 :(得分:0)

在第三个查询中,不清楚哪个列在哪个表中。由于您说所有结果都需要显示在一行中,我假设s.staffNo = g.staffNo仅对一个行对为true,并且c.catID = p.catID仅对一个行对为true。然后你可以使用JOIN。我建议添加短表名称。 UNION不起作用,因为它用于组合相等长度和相等列类型的行集。

修改:添加了c.p.请不要使用mysql_query,已弃用。请改用mysqli_query。我不确定这个数据库是否以最佳方式设计。你不能将所有内容合并到tblstaff吗?

SELECT s.staffName, s.staffNo, g.grade, g.gradePosition, g.gradeDepartment, 
c.catTechnical, c.catOtherTechnical, c.catTechnicalDescription, c.catOtherTechnicalDescription, c.catWeightage, p.perReqScore, p.perActScore, p.perAction, p.perOtherAction, p.perTrainingIlsas, p.perTrainingPublic
FROM tblstaff s
JOIN tblgrade g ON s.staffNo = g.staffNo
JOIN tblcategory c
JOIN tblperformance p on c.catID = p.catID

答案 1 :(得分:0)

false