select distinct * from ( select * from A union all select * from B ) t
select * from a union all select * from b as t where not exists(select 1 from a where x = t.x)
这个不行,没有包含TA的全部记录SELECT * FROM TA UNION SELECT * FROM TB
select * from A union all ( select * from B b where not exists (select * from A where X=b.x) )
select t.* into C from (select * from A union all select * from B where not exists(select 1 from A where X=B.X)) t
select * from tba union select * from tbb where tbb.x not in (select distinct x from tba)
declare @tablea table (X varchar(12)) insert into @tablea select '4' union all select '5' union all select '6' union all select '7'--select * from @tableadeclare @tableb table (X varchar(12)) insert into @tableb select '5' union all select '8' union all select '9'--select * from @tablebdeclare @tablec table (X varchar(12))insert into @tablec select distinct * from ( select * from @tablea union all select * from @tableb ) tselect * from @tablec/* X ------------ 4 5 6 7 8 9 */
declare @tablea table (X varchar(12)) insert into @tablea select '4' union all select '5' union all select '6' union all select '7' declare @tableb table (X varchar(12)) insert into @tableb select '5' union all select '8' union all select '9'select * from @tablea UNION select * from @tableb /*X ------------ 4 5 6 7 8 9 */
这句是不是有问题啊?当X取一个A、B表都有的,不就没有记录插入C表了吗?
--不好意思,是我理解错了,应该这样理解: select t.* into C from (select * from A union all /**/select * from B where not exists(select 1 from A where X=B.X)/**/ ) t --应该是2个/**/之间的和前面的UNION ALL
SELECT * INTO C ( SELECT * FROM TA UNION SELECT * FROM TB ) AS T
select t.* into C from (select * from A union all /**/select * from B where not exists(select 1 from A where X=B.X)/**/ ) t
SQL codeselect * from A union all ( select * from B b where not exists (select * from A where X=b.x) ) 关注
UNION
SELECT * FROM TB
from
(
select * from A
union all
select * from B
) t
from a
union all
select *
from b as t
where not exists(select 1 from a where x = t.x)
UNION
SELECT * FROM TB
union all
(
select * from B b where not exists (select * from A where X=b.x)
)
into C
from
(select * from A union all select * from B where not exists(select 1 from A where X=B.X)) t
union
select * from tbb where tbb.x not in (select distinct x from tba)
declare @tablea table (X varchar(12))
insert into @tablea
select '4' union all
select '5' union all
select '6' union all
select '7'--select * from @tableadeclare @tableb table (X varchar(12))
insert into @tableb
select '5' union all
select '8' union all
select '9'--select * from @tablebdeclare @tablec table (X varchar(12))insert into @tablec
select distinct *
from
(
select * from @tablea
union all
select * from @tableb
) tselect * from @tablec/*
X
------------
4
5
6
7
8
9
*/
insert into @tablea
select '4' union all
select '5' union all
select '6' union all
select '7'
declare @tableb table (X varchar(12))
insert into @tableb
select '5' union all
select '8' union all
select '9'select * from @tablea
UNION
select * from @tableb
/*X
------------
4
5
6
7
8
9
*/
select t.*
into C
from
(select * from A union all /**/select * from B where not exists(select 1 from A where X=B.X)/**/ ) t
--应该是2个/**/之间的和前面的UNION ALL
SELECT * INTO C
(
SELECT * FROM TA
UNION
SELECT * FROM TB
) AS T
into C
from
(select * from A union all /**/select * from B where not exists(select 1 from A where X=B.X)/**/ ) t
union all
(
select * from B b where not exists (select * from A where X=b.x)
) 关注