日期:2014-05-17  浏览次数:20358 次

帮忙看个SQL 排除查询
表A

ProID ProName UserType UserID
-----------------------------
1     '产品A'  1        0
1     '产品A'  3        0
1     '产品A'  5        0


表B

ProID ProName UserType UserID
-----------------------------
1     '产品A'  1        0
1     '产品A'  3        0
1     '产品A'  5        0
1     '产品A'  0        27
1     '产品A'  0        33

要找出在表B中排除表A的数据.比较的字段是 ProID相同,UserType 和 UserID 同时不同

结果是
ProID ProName UserType UserID
-----------------------------
1     '产品A'  0        27
1     '产品A'  0        33


select * from 表B as B where ProID=1
and not exists
(
 select 1 from 表A where ProID=B.ProID and UserType=B.UserTye and UserID=B.UserID 
)



不知道是不是写错了,结果得不到?

------解决方案--------------------
select * from t2 as B where ProID=1 and not exists 
(  select 1 from t1 where ProID=B.ProID and UserType=B.UserType and UserID=B.UserID  ) 
你的列名称没写对吧!
------解决方案--------------------
UserType=B.UserTye
=>
UserType=B.UserType
------解决方案--------------------

select b.* from 表B as B,表A as A where B.ProID=1
and  A.ProID=B.ProID and UserType<>B.UserTye and UserID<>B.UserID 
)