Home >Database >Mysql Tutorial >How to Join Three Tables in MySQL to Display Specific Results?

How to Join Three Tables in MySQL to Display Specific Results?

Barbara Streisand
Barbara StreisandOriginal
2024-12-16 01:49:091041browse

How to Join Three Tables in MySQL to Display Specific Results?

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:

  1. 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
  2. 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:

  1. 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
  2. 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn