SQL语句不存在于

时间:2013-12-22 19:48:40

标签: sql

我想从第一个表中选择第二个表中没有条目的条目

我试过了:

SELECT
    first.clientid
FROM
    table1 AS first,
    table2 AS second
WHERE
    first.clientid NOT IN second.clientid

但我已经意识到问题

任何提示?

2 个答案:

答案 0 :(得分:3)

错误的语法。

使用IN:

SELECT clientid FROM table1
WHERE clientid NOT IN (SELECT clientid FROM table2);

或使用EXISTS:

SELECT clientid FROM table1
WHERE NOT EXISTS (SELECT * FROM table2 where table2.clientid = table1.clientid);

或者使用MINUS(在每个DBMS中都不可用):

SELECT clientid FROM table1
MINUS
SELECT clientid FROM table2;

答案 1 :(得分:1)

您可以使用NOT EXISTS来执行此操作:

SELECT first.clientid 
FROM table1 AS first
WHERE NOT EXISTS 
    (SELECT * FROM table2 AS second WHERE first.clientid = second.clientid)