--> 测试数据: @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 */
--> 测试数据: @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
*/
我同学让我帮忙问下的,估计是一个面试题,我sql这块比较水
请问这种问题您是一般遇到了解决后,写在自己的文档中么,我看您每次回答问题都特别快
A表中不包含B表的记录:
select * from a
except
select * from b
B表中不包含A表的记录:
select * from b
except
select * from a
如果两表字段不一样,那么就需要使用not exists来写了。