日期:2014-05-18  浏览次数:20829 次

求助:sql取两个查询语句的差集
同一个表,如何取以下两个查询结果的差集

select O.boardNumID from Orders as O where O.StationId=39 and o.StatusId =1;

select 0.boardNumID from Orders as O where O.StationId=39 and o.StatusId =0;



------解决方案--------------------
你的差集是什么?
这两个查询语句,所获得的结果集应该没有交集.
------解决方案--------------------
SQL code
select O.boardNumID from Orders as O where O.StationId=39 and o.StatusId =1;
except
select 0.boardNumID from Orders as O where O.StationId=39 and o.StatusId =0;

------解决方案--------------------
探讨
SQL code
select O.boardNumID from Orders as O where O.StationId=39 and o.StatusId =1
except
select 0.boardNumID from Orders as O where O.StationId=39 and o.StatusId =0


------解决方案--------------------
SQL code
select O.boardNumID-(
select boardNumID from Orders  where StationId=39 and StatusId =0 and  id=o.id)--id为唯一值 
from Orders as O where O.StationId=39 and o.StatusId =1;

------解决方案--------------------
SQL code
select 
 O.boardNumID 
from 
 Orders as O 
where
 O.StationId=39 and o.StatusId =1
and
  NOT IEXISTS(select 1 from Orders  where boardNumID=O.boardNumID StationId=39 and StatusId =0)