Sebastian Jeliazkov Sebastian Jeliazkov - 4 months ago 7
SQL Question

Query 2 tables with left join that id dont exist in 3rd table

I got


table1 table2 table3

id id id
name table1id customerid
table3id


How can i fetch the table1.name joining table2 where customerid = someid and table3id not exist in table2

Answer
    SELECT
       t1.id
       t1.name
    FROM
       Table1 t1
       LEFT JOIN Table2 t2
       on t1.id = t2.table1id
       LEFT JOIN Table3 t3
       on t2.table3id = t3.id
       AND t3.customerid = 93
    WHERE
       t3.id IS NULL