当前位置: 代码迷 >> Sql Server >> sql小疑点
  详细解决方案

sql小疑点

热度:99   发布时间:2016-04-27 13:06:35.0
sql小问题。
在SQL数据库中,如果有两张表(A、B表),A表有5万条数据,B表有3万条数据,两表中有一万条相同的数据,找出两表中不相同的数据来。怎么做?


------解决方案--------------------
SQL code
--> 测试数据: @adeclare @a table (id int,col varchar(1))insert into @aselect 1,'a' union allselect 2,'b' union allselect 3,'c'--> 测试数据: @bdeclare @b table (id int,col varchar(1))insert into @bselect 2,'b' union allselect 3,'c' union allselect 4,'d'select * from @a a full join @b b on a.id=b.id and a.col=b.colwhere a.id+b.id is null/*id          col  id          col----------- ---- ----------- ----1           a    NULL        NULLNULL        NULL 4           d*/
------解决方案--------------------
如果两表字段一样,那么:
A表中不包含B表的记录:
select * from a 
except 
select * from b
B表中不包含A表的记录:
select * from b
except 
select * from a
如果两表字段不一样,那么就需要使用not exists来写了。
  相关解决方案