declare @A table(ID int,name varchar(20))
insert @A values(1,'aaa')
insert @A values(2,'bbb')
insert @A values(3,'ccc')declare @B table(ID int,name varchar(20))
insert @B values(1,'aaa')
insert @B values(2,'bbb')
insert @B values(4,'ddd')select a.* from @A a where a.id not in(select id from @B)
select a.* from @B a where a.id not in(select id from @A)

解决方案 »

  1.   

    (1)
    select a.* from a
    where not exists(
        select 1 from b
        where a.id=b.id and a.name=b.name
    )(2)
    select a.* from a
    where not exists(
        select 1 from b
        where a.id=b.id and a.name=b.name
    )
      

  2.   


    select * from (select * from A union all select * from B) a group by ID,name having count(*)=1
      

  3.   

    select a.* from a where not exists(select 1 from b where a.id=b.id and a.name=b.name)
    UNION ALL
    select a.* from a where not exists(select 1 from b where a.id=b.id and a.name=b.name)