Joining Three Tables Using MySQL
To join three tables and display the results in a specific format, follow these steps:
Steps for Joining Student, Course, and Bridge Tables:
Start with the ANSI-standard SQL syntax for joining tables:
SELECT s.name AS Student, c.name AS Course FROM student s INNER JOIN bridge b ON s.id = b.sid INNER JOIN course c ON b.cid = c.id
This query will correctly join the tables and display the required result:
| Student | Course | |---|---| | ahmed | physic | | ahmed | maths | | ahmed | computer | | ahmed | chemistry | | ali | physic | | ali | maths | | john | computer | | john | chemistry | | king | physic | | king | maths |
Steps for Joining employee and manage Tables:
Use the following query to join the employee and manage tables:
SELECT e1.name AS Manager, e2.name AS Staff FROM employee e1 INNER JOIN manage m ON e1.id = m.mid INNER JOIN employee e2 ON m.eid = e2.id
This query will return the desired result:
| Manager | Staff | |---|---| | ali | king | | ali | mak | | mak | sam | | sam | jon |
The above is the detailed content of How to Join Three Tables in MySQL to Display Specific Results?. For more information, please follow other related articles on the PHP Chinese website!