我现在有两个表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型的原因?该怎么修改,谢谢!

解决方案 »

  1.   

    ---try
    select * from (select * from A
    union
    select * from B) a
    group by aid
    having count(*)=1
      

  2.   

    上面的执行结果是包含 UNION 运算符的查询表达式中的所有查询都必须在选择列表中包含同样数目的表达式。
      

  3.   

    try:
    select aid from a where not exists (select 1 from b where bid=a.aid)比较下
    select distinct aid from aselect distinct bid from b
    两个语句返回的记录数是否相同,如果是,说明两表数据本来就是相同的
      

  4.   

    select aid from a where aid not in (select 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
      

  5.   

    还可以这样:
    select a.* from ta a left join tb b on a.aid = b.bid and b.bid is null
    ......................
    这个我看不怎么懂 
    刚才在查询分析器里试了下 好像不怎么对
      

  6.   

    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
      

  7.   

    谢谢各位老大,我在导数据的时候没有注意,因为前面的几行是两位数字,结果后面的数据都成了null,再次谢谢!
      

  8.   

    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
    -----------------------------------------------------------------------------------
    多谢i9988(冒牌j9988 V0.3)朋友的指正!