查询具有左连接的2个表,该表在第3个表中不存在

时间:2016-07-09 16:53:34

标签: sql

我有

table1    table2       table3

id         id           id
name       table1id     customerid
           table3id

如何获取table1.name加入table2,其中customerid = someid,table3id不存在于table2中

2 个答案:

答案 0 :(得分:1)

    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

答案 1 :(得分:0)

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