我想在查询的in子句中缺少值
人
ID | Name | location_id |
1 | Rex| 5 |
2 | Tex| 7 |
3 | Jim| 8 |
4 | John| 11 |
5 | Tom| 12 |
放置
location_id | Place |
5 | New York |
6 | London |
7 | Madrid |
8 | Paris |
9 | Dublin |
10 | Chicago |
11 | Berlin |
12 | Colombo |
我想查找没有人的地方,当我在查询的in子句中给出一个位置列表
我不想找到所有不在人员表中的位置,只有那些我在in子句中提供的位置
查询
SELECT location_id FROM people WHERE location_id in (5,6,7,8,9);
预期输出
|Location_id|
6|
9|
答案 0 :(得分:1)
使用JOIN
代替两个IN
操作(提高了性能)。
试试这个:
SELECT pl.location_id
FROM place pl LEFT JOIN
people pp ON pl.location_id=pp.location_id
WHERE pp.location_id IS NULL
AND pl.location_id IN (5,6,7,8,9);
结果:
LOCATION_ID
6
9
请参阅SQL Fiddle中的结果。
修改强>
不使用JOIN
:
SELECT location_id
FROM place
WHERE location_id NOT IN
(SELECT location_id FROM people)
AND pl.location_id IN (5,6,7,8,9);
答案 1 :(得分:0)
SELECT location_id FROM place WHERE location_id NOT IN (SELECT location_id FROM people) AND location_id IN (5,6,7,8,9);
答案 2 :(得分:0)
尝试以下查询
select place from place
inner join poeple on place.location_id <> people.location_id where AND
place.location_id IN (5,6,7,8,9);
Inner join
用于加入两个或更多表格。
答案 3 :(得分:0)
select p.location_id FROM people p join Place l on
p.location_id!=l.location_id WHERE p.location_id in (5,6,7,8,9);
答案 4 :(得分:0)
从p.location_id = l.location_id中的人们左边的连接位置l中选择l.location_id,其中p.location_id不在(5,6,7,8,9)