1

可能重复:
需要 SQL 查询来查找没有子记录的父记录

我有一张桌子和一张桌子,

select name from one

假设我有 3 行 a、b、c

select name from table two;

假设我在表 2 中有 3 行,其中包含 a、d、f

我想select * from table onename 不在表二中,所以我应该以 b,c 而不是 a 结尾,因为 a 在表二中

请指教谢谢

4

2 回答 2

3

Use a NOT EXISTS clause

SELECT name from one
WHERE NOT EXISTS (
    SELECT 1 FROM two
    WHERE two.name = one.name
)

Update

Alternatively, you can use a LEFT JOIN with a NULL qualifier

SELECT one.name FROM one
LEFT JOIN two on one.name = two.name
WHERE two.name IS NULL
于 2012-12-05T01:37:17.480 回答
3

You can make a subquery to get the names from table two and use them to exlude rows from table one

SELECT * FROM table1 WHERE name NOT IN (SELECT name FROM table2)

于 2012-12-05T01:39:55.887 回答