我在w3schools遇到not existssql 查询问题
我希望select所有与shipperid = 1但不合作的客户shipperid = 3。我尝试了以下方法:
select o1.customerid, o1.shipperid
from orders o1
where o1.shipperid=1 and not exists
(select o2.customerid from orders o2
where o1.orderid=o2.orderid
and o2.shipperid=3)
order by customerid
;
上面的查询给出了所有与之合作的客户,shipperid = 1并且不排除与之合作的客户shipperid = 3。查询不正确的地方。(我需要特别使用not exists)
PS:我知道in解决方案:
select customerid, shipperid
from orders
where shipperid=1 and customerid not in (
select customerid
from orders
where shipperid=3
)
order by customerid;
为什么not exists解决方案不起作用?