简体   繁体   中英

how to combine multiple mySQL queries?

I'm newbie in mySQL. I have multiple queries sql to combine and I'm using php and mySQL. How can I combine the query? All result of the query I need to display in one row of a table.

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

This is my database tables.

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

This is my current code and database table. I need to combine query 1 and query 2 because I want to display the result in one row of a table by staffNo. I means one staffNo for one row in a table. Then other staffNo will display into a new row of the same table.

In your third query, it's not clear which columns are in which table. Since you say that all results need to display in one row, I assume s.staffNo = g.staffNo is true only for one row pair, and also that c.catID = p.catID is true for only one row pair. Then you can just use JOIN. I would suggest adding the short table names though. UNION will not work because that is for combining row sets of equal length and equal column types.

Edit: Added c. and p. Please don't use mysql_query , it's deprecated. Use mysqli_query instead. I'm not sure if this database is designed in the best way possible. Can't you just merge everything into 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;

you have done all already except 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM