【问题标题】:how to combine multiple mySQL queries?如何组合多个 mySQL 查询?
【发布时间】: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 的行

标签: php mysql select


【解决方案1】:

在您的第三个查询中,不清楚哪些列在哪个表中。既然您说所有结果都需要显示在一行中,我假设 s.staffNo = g.staffNo 仅适用于一对行,并且 c.catID = p.catID 仅适用于一对行。然后你可以使用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

【讨论】:

  • 很抱歉没有在问题中提及。我的第三个查询是从 tblcategory 和 staffNo 的 tblperformance 两个表中加入。如果您想查看我的代码,我已经更新了我之前的问题。时长
【解决方案2】:

除了join tblperformance p on s.staffNo=p.staffNo之外,你都已经完成了

SELECT s.staffName, s.staffNo, g.grade, g.gradePosition, g.gradeDepartment 
c.catTechnical, c.catOtherTechnical, c.catTechnicalDescription,
c.catOtherTechnicalDescription, c.catWeightage, p.perReqScore

FROM tblstaff s
join tblgrade g on s.staffNo=g.staffNo
join tblperformance p on s.staffNo=p.staffNo
join tblcategory c on p on c.catID=p.catID

【讨论】:

    【解决方案3】:
    SELECT s.staffName, s.staffNo, g.grade, g.gradePosition, g.gradeDepartment,
       c.catTechnical, c.catOtherTechnical, c.catTechnicalDescription, 
       c.catOtherTechnicalDescription, c.catWeightage, c.perReqScore, c.perActScore, 
       c.perAction, c.perOtherAction, c.perTrainingIlsas, c.perTrainingPublic
    FROM tblstaff s, tblgrade g, tblcategory c
    JOIN tblperformance p on c.catID = p.catID
    WHERE s.staffNo = g.staffNo;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-02
      • 1970-01-01
      相关资源
      最近更新 更多