Sebastian Jeliazkov
Sebastian Jeliazkov

Reputation: 91

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

Upvotes: 0

Views: 42

Answers (2)

Matt
Matt

Reputation: 14341

    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

Upvotes: 1

Robert Columbia
Robert Columbia

Reputation: 6418

SELECT table1.name
INNER JOIN table2 ON table1.id = table2.table1id
WHERE table2.id NOT IN (SELECT id FROM table3)

Upvotes: 0

Related Questions