Becky
Becky

Reputation: 5585

Connect to multiple tables

How can I connect to 4 tables in a single query using forign key IDs?

I know how to connect to two tables.

$sql = "SELECT tb1.id, tb2.name FROM tblA tbl1 LEFT JOIN tblB tbl2 ON tb1.id = tbl2.studentID ORDER BY tbl1.id DESC LIMIT 20";
$statement = $con_db->prepare($sql);

Upvotes: 0

Views: 89

Answers (1)

Sardor Dushamov
Sardor Dushamov

Reputation: 1667

Try like this :

select t1.ID, t2.studentID, t3.aID, t4.bID 
from  table1 as t1

left join tbl2 as t2 on t2.studentID = t1.id
left join tbl3 as t3 on t3.aID = t1.id
left join tbl4 as t4 on t4.bID = t1.id

Upvotes: 2

Related Questions