两个表的结构完全相同,如何找出的差异记录。
A表
N      M
aa     131
bb     311
cc     3124
aa     581
.
.
.A表
N      M
aa     213
bb     311
cc     3124
aa     581
dd     100
.
.
.如何找出两表中不相同的记录。

解决方案 »

  1.   

    select * from a
    where not exists(select 1 from b where a.n=b.n and a.m=b.m)
    union
    select * from b
    where not exists(select 1 from a where a.n=b.n and a.m=b.m)
      

  2.   

    如何找出A表与B表中列N相同而列M不同的记录?
    我加分啊。
      

  3.   


    select * from a ,b where a.n=b.n and a.m<>b.m
      

  4.   

    select * from A inner join B on A.N = B.N
    where A.M <> B.M
      

  5.   

    select * from a 
    where exists(select 1 from b where a.n=b.n and a.m<>b.m) 
    union all
    select * from b 
    where exists(select 1 from a where a.n=b.n and a.m<>b.m)
      

  6.   

    如何找出两表中不相同的记录。 select * from a
    minus
    select * from b
    union all
    select * from b
    minus
    select * from a
      

  7.   

    2005:
    select * from (select * from  a except select * from  b)t
    union all
    select * from (select * from  b except select * from  a)t
      

  8.   

    2000:checksum/binary_checksum
    select * from  a where checksum (*) not in(select checksum (*) from  b)
    union all
    select * from  b where checksum (*) not in(select checksum (*) from  a)
      

  9.   

    use MyFiles
    set nocount on
    if object_id('tempdb.dbo.#a') is not null drop table #a
    if object_id('tempdb.dbo.#b') is not null drop table #b---------------例子数据 begin---------------
    create table #a (N varchar,M int)
    insert into #a 
    select 'a',131 union all
    select 'b',311 union all
    select 'c',3124 union all
    select 'a',581create table #b (N varchar,M int)
    insert into #b 
    select 'a',213 union all
    select 'b',311 union all
    select 'c',3124 union all
    select 'a',581 union all
    select 'd',100 
    ---------------例子数据   end------------------------------源程序    begin--------------
    select * from #a
    select * from #b
    select * from #a a where not exists (select 1 from #b where a.N=N and a.M=M) union
    select * from #b b where not exists (select 1 from #a where N=b.N and M=b.M)
    drop table #a,#b
    set nocount off
    ---------------源程序     end---------------/*****************************
      N   M
    1 a  131
    2 a  213      
    3 b  100     
    *****************************/
      

  10.   

    程序超复杂
    两个问题:
    1.不知道怎么输入aa,只好用a代替,晕倒。
    2.checksum(*)可以解决问题。
    3.程序太臃肿了。
      select * from #a a where not exists (select 1 from #b where a.N=N and a.M=M) union
      select * from #b b where not exists (select 1 from #a where N=b.N and M=b.M) 默默努力中
      

  11.   

    幸亏你只有两列,再多了还得union all,更麻烦