santa santa - 4 months ago 8
SQL Question

Selecting from multiple tables with LEFT JOIN

I have 3 tables

t1 (select these records)
-------------
id
offer_id
business_id

t2 (offer details)
-------------
id
offer_details
business_id


t3 (business details)
-------------
id
business_name


I need to select all records from t1 and add information from t2 and t3. Seems basic but I can't seem to be able to get it right -- must be the heat.

SELECT t2.offer_details, t3.business_name
FROM t2
LEFT JOIN t1 ON (t1.offer_id = t2.id)
LEFT JOIN t3 ON (t1.business_id = t3.id)

Answer

should be

 SELECT t2.offer_details, t3.business_name
 FROM t1
 LEFT JOIN t1 ON (t1.offer_id = t2.id)
 LEFT JOIN t3 ON (t1.business_id = t3.id)

Your lead table is t1 and the join should be based on this table