C# 两个datatable合并取并集减交集请问该段代码怎么写?
第一个DataTable某个字段有1,2,3,4,第二个DataTable相同字段有3,4,5,6,我最后想要的是1,2,5,6的记录,请问这段代码该怎么写?...
第一个DataTable某个字段有1,2,3,4,第二个DataTable相同字段有3,4,5,6,我最后想要的是1,2,5,6的记录,请问这段代码该怎么写?
展开
2个回答
展开全部
--测试表
create table testA(a int)
create table testB(a int)
insert into testA select 1 union select 2 union select 3 union select 4
insert into testB select 3 union select 4 union select 5 union select 6
--第一种
SELECT a FROM testA WHERE a NOT IN(SELECT a FROM testB )
UNION
SELECT a FROM testb WHERE a NOT IN(SELECT a FROM testA )
--第二种
SELECT a FROM testA EXCEPT SELECT a FROM testB
UNION
(SELECT a FROM testB EXCEPT SELECT a FROM testA)
--第三种
SELECT a FROM testA UNION SELECT a FROM testB
EXCEPT
(SELECT a FROM testA INTERSECT SELECT a FROM testB)
create table testA(a int)
create table testB(a int)
insert into testA select 1 union select 2 union select 3 union select 4
insert into testB select 3 union select 4 union select 5 union select 6
--第一种
SELECT a FROM testA WHERE a NOT IN(SELECT a FROM testB )
UNION
SELECT a FROM testb WHERE a NOT IN(SELECT a FROM testA )
--第二种
SELECT a FROM testA EXCEPT SELECT a FROM testB
UNION
(SELECT a FROM testB EXCEPT SELECT a FROM testA)
--第三种
SELECT a FROM testA UNION SELECT a FROM testB
EXCEPT
(SELECT a FROM testA INTERSECT SELECT a FROM testB)
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询