1

I'm creating sql command where the date stored in different table with foreign key of borrowers_id. I can execute and output left join of 2 tables but what if I have 3 or more tables fetching the datas of my foreign key.

Here's my SQL command,

SELECT borrowers.firstname,
borrowers.middlename,
borrowers.lastname,
borrowers.home_address,
borrowers.date_of_birth,
borrowers.age,
borrowers.residential_status,
borrowers.status,
borrowers.date_added,
borrowersp.spouse_name,
borrowersp.date_of_birth,
borrowersp.age 
FROM tblborrowers as borrowers LEFT JOIN tblborrowerspouse as borrowersp 
ON borrowers.borrowers_id = borrowersp.borrowers_id 
WHERE borrowers.borrowers_id=23432413;
Preston Guillot
  • 6,493
  • 3
  • 30
  • 40
truelogicINC
  • 119
  • 2
  • 14

2 Answers2

1

You can add more tables by continuing to join:

FROM tblborrowers as borrowers LEFT JOIN tblborrowerspouse as borrowersp 
ON borrowers.borrowers_id = borrowersp.borrowers_id 
LEFT JOIN tblborrowerskid as kid
ON borrowers.borrowers_id = kid.parent_id

WHERE borrowers.borrowers_id=23432413;

You can add as many as you'd need.

Tom Studee
  • 10,316
  • 4
  • 38
  • 42
1

Lets say you have tables : t1 ,t2 and t3 ..Left Joining :

SELECT        t2.id, t1.id AS Expr1
FROM            t3 LEFT JOIN
                         t1 ON t3.id = t1.id LEFT JOIN
                         t2 ON t1.id = t2.id

We can also write the above query with left outer join :

SELECT        t2.id, t1.id AS Expr1
FROM            t3 LEFT OUTER JOIN
                         t1 ON t3.id = t1.id LEFT OUTER JOIN
                         t2 ON t1.id = t2.id

For 5 tables ..t1,t2,t3,t4 and t5 :

SELECT        t2.id, t1.id AS Expr1, t5.id AS Expr2, t4.id AS Expr3
FROM            t5 LEFT OUTER JOIN
                         t1 ON t5.id = t1.id LEFT OUTER JOIN
                         t4 ON t1.id = t4.id LEFT OUTER JOIN
                         t3 ON t1.id = t3.id LEFT OUTER JOIN
                         t2 ON t1.id = t2.id

Please have a look at this SO post : What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?

Community
  • 1
  • 1
Tharif
  • 13,794
  • 9
  • 55
  • 77