我正在尝试运行此查询,它应该让所有在wh_route中不是驱动程序的员工,驱动程序是fk for
以下查询正在运行并向我提供结果:
select distinct driver from wh_route;
select id from pa_employee o where o.id not in (1,2,3,4,5);
但是当我尝试这样做时:
select id from pa_employee o where o.id not in (select distinct driver from wh_route);
它不会返回任何结果。
表格供参考:
WH_ROUTE
id driver
1 1
2 2
PA_EMPLOYEE
id
1
2
3
预期的行为是它返回id 3,但它不返回任何内容
答案 0 :(得分:1)
NOT IN
有点棘手。请尝试NOT EXISTS
:
select id from pa_employee o
where o.id not exists (select * from wh_route where driver = o.id);
答案 1 :(得分:1)
刚想通了:在wh_route表中,驱动程序有一个空值,因此工作查询变为
select id
from pa_employee o
where o.id not in (
select distinct driver
from wh_route
where id is not null);
答案 2 :(得分:0)
您可能还想删除子SELECT中的DISTINCT,因为它不需要。这可能会加快您的查询速度。