在SQL数据库中,如果有两张表(A、B表),A表有5万条数据,B表有3万条数据,两表中有一万条相同的数据,找出两表中不相同的数据来。怎么做?

解决方案 »

  1.   


    --> 测试数据: @a
    declare @a table (id int,col varchar(1))
    insert into @a
    select 1,'a' union all
    select 2,'b' union all
    select 3,'c'
    --> 测试数据: @b
    declare @b table (id int,col varchar(1))
    insert into @b
    select 2,'b' union all
    select 3,'c' union all
    select 4,'d'select * from @a a full join @b b on a.id=b.id and a.col=b.col
    where a.id+b.id is null/*
    id          col  id          col
    ----------- ---- ----------- ----
    1           a    NULL        NULL
    NULL        NULL 4           d
    */
      

  2.   


    我同学让我帮忙问下的,估计是一个面试题,我sql这块比较水
    请问这种问题您是一般遇到了解决后,写在自己的文档中么,我看您每次回答问题都特别快
      

  3.   

    如果两表字段一样,那么:
    A表中不包含B表的记录:
    select * from a 
    except 
    select * from b
    B表中不包含A表的记录:
    select * from b
    except 
    select * from a
    如果两表字段不一样,那么就需要使用not exists来写了。
      

  4.   

    建一个checksum(col1,col2,col3)列,然后比较这个值?