Robert Dickey
Robert Dickey

Reputation: 834

MySQL Join Query with Multiple Joins

I have three different tables I am trying to pull info from. Related in different ways. I am trying to find a Join statement that will allow me to pull the information a little better than using the two queries I am now.

Here is what I have

SELECT * FROM reports LEFT JOIN students USING (sID)

This allows me to properly merge the two tables. However - There is a row in the reports table named pID. I have another table named test, that also has a matching pID row, along with other data. I am trying to access the data from that table as well. Is it possible to join three tables in that way? Not only getting data from the sID, but the pID as well

Upvotes: 2

Views: 1528

Answers (2)

Fil
Fil

Reputation: 8873

Hi robert yes it is posible. You can do a query like as shown below given your three tables, reports, students, and test.

SELECT *
FROM `reports`
LEFT JOIN `students` ON `students.sid=reports.sid`
LEFT JOIN `test` ON `reports.pid=tests.pid`

Upvotes: 2

Chin Leung
Chin Leung

Reputation: 14941

You can do the following

SELECT *
FROM reports
LEFT JOIN students USING (sID)
LEFT JOIN table3 USING (pID)

Where table3 is the name of your table.

Upvotes: 4

Related Questions