我现在有两个表A,B
A 中的数据包含 B 中的所有数据
现在怎么查出 A 中有但 B 中没有的数据我的 A 表中 aid 和 B 表中的 bid 的字段都是 nvarchar 型的
我用select aid from a where aid not in (select bid from b)
返回的结果是:影响的行数为0请问是不是nvarchar型的原因?该怎么修改,谢谢!
A 中的数据包含 B 中的所有数据
现在怎么查出 A 中有但 B 中没有的数据我的 A 表中 aid 和 B 表中的 bid 的字段都是 nvarchar 型的
我用select aid from a where aid not in (select bid from b)
返回的结果是:影响的行数为0请问是不是nvarchar型的原因?该怎么修改,谢谢!
select * from (select * from A
union
select * from B) a
group by aid
having count(*)=1
select aid from a where not exists (select 1 from b where bid=a.aid)比较下
select distinct aid from aselect distinct bid from b
两个语句返回的记录数是否相同,如果是,说明两表数据本来就是相同的
---------------------------------------------------------------------------------
请楼主切记:使用not in(子查询)进行比较时,一定要在子查询中排除空值NULL,这样改一下试试:
select aid from a where aid not in
(select bid from b WHERE bid IS NOT NULL ) /*加上is not null排除空值*/还可以这样:
select a.* from ta a left join tb b on a.aid = b.bid and b.bid is null
select a.* from ta a left join tb b on a.aid = b.bid and b.bid is null
......................
这个我看不怎么懂
刚才在查询分析器里试了下 好像不怎么对
写错了,应该
select a.* from ta a left join tb b on a.aid = b.bid where b.bid is null
写错了,应该
select a.* from ta a left join tb b on a.aid = b.bid where b.bid is null
-----------------------------------------------------------------------------------
多谢i9988(冒牌j9988 V0.3)朋友的指正!