簡體   English   中英

如何結合多個MySQL查詢?

[英]how to combine multiple mySQL queries?

我是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 } ?> 

這是我的數據庫表。

員工

 +---------+-----------+ | staffNo | staffName | +---------+-----------+ | 1002435 | Fadzlan | +---------+-----------+ 

特級

 +---------+---------+-------+---------------+-----------------+ | gradeID | staffNo | grade | gradePosition | gradeDepartment | +---------+---------+-------+---------------+-----------------+ | 1 | 1002435 | E14 | Manager | IB | +---------+---------+-------+---------------+-----------------+ 

類別

 +-------+--------------+---------------------------+-------------------------+--------------+ | catID | catTechnical | catOtherTechnical | catTechnicalDescription | catWeightage | +-------+--------------+---------------------------+-------------------------+--------------+ | 18 | Project(18) | Project Coordination(181) | 30 | | +-------+--------------+---------------------------+-------------------------+--------------+ 

性能

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

這是我當前的代碼和數據庫表。 我需要組合查詢1和查詢2,因為我想按staffNo在表的一行中顯示結果。 我的意思是一張桌子上一行沒有員工。 然后其他人員編號將顯示在同一表格的新行中。

在第三個查詢中,不清楚哪個表中的列。 由於您說所有結果都需要顯示在一行中,因此我假設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
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;

除了join tblperformance p on s.staffNo=p.staffNo外,您已經完成了所有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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM