从表中选择列值在另一个表中不存在的位置

时间:2013-04-23 21:50:13

标签: mysql sql

SELECT  Id, Name, Lastname
FROM customers AS c, Places AS p, 
WHERE c.customer_ID = p.customer_ID

我的问题是,我想阻止查询的结果显示另一个表(阶段)中存在的行

4 个答案:

答案 0 :(得分:6)

您可以执行LEFT JOIN并检查是否为空。

SELECT  Id, Name, Lastname
FROM customers AS c LEFT JOIN Places AS p ON c.customer_ID = p.customer_ID
WHERE p.customer_ID IS NULL

答案 1 :(得分:1)

添加

and not exists
(subquery to select your exclusions)

到您的查询

答案 2 :(得分:0)

您可以使用:

SELECT  Id, Name, Lastname
FROM customers AS c JOIN Places AS p USING(customer_ID)

使用in/exists这是一种更快捷的方式。

答案 3 :(得分:0)

我想你想要这样的东西:

SELECT Id, Name, Lastname FROM customers, WHERE customer_ID NOT IN (SELECT customer_ID FROM Places)