从表中选择不在当前查询结果中的所有行

时间:2017-12-18 03:50:15

标签: mysql

我有这个问题:

SELECT * FROM 
dispensaries_list_new d JOIN cities c ON d.city = c.city_name
AND d.state = c.state_code

我想取所有行,不包括此查询的结果。我试过不等于。但返回错误数据。

1 个答案:

答案 0 :(得分:0)

如果您想让拥有匹配城市的药房,那么您可以使用left joinwhere

SELECT d.*
FROM dispensaries_list_new d LEFT JOIN
     cities c
     ON d.city = c.city_name AND d.state = c.state_code
WHERE c.city_name IS NULL;

实际上,如果你想要没有药房的城市,那么你可以使用逆逻辑 - 基本上颠倒了连接的顺序:

SELECT c.*
FROM cities c LEFT JOIN
     dispensaries_list_new d 
     ON d.city = c.city_name AND d.state = c.state_code
WHERE d.city IS NULL;