先描述一下情况有表T1,T2,列数是一样的,
T1:
a b c d
1 1 2 2
2 2 4 4
3 3 6 6T2:
a b c d
1 2 3 4
2 2 3 4我想以列a为关联,把T1和T2合并,去掉T1中和T2重复的行,得到以下结果T2T2:
a b c d
1 2 3 4
2 2 3 4
3 3 6 6
求完整的sql语句...
T1:
a b c d
1 1 2 2
2 2 4 4
3 3 6 6T2:
a b c d
1 2 3 4
2 2 3 4我想以列a为关联,把T1和T2合并,去掉T1中和T2重复的行,得到以下结果T2T2:
a b c d
1 2 3 4
2 2 3 4
3 3 6 6
求完整的sql语句...
create table t1(a int,b int,c int,d int)
insert into t1
select 1 ,1 ,2 ,2 union all
select 2 ,2 ,4 ,4 union all
select 3 ,3 ,6 ,6
gocreate table t2(a int,b int,c int,d int)
insert into t2
select 1 ,2 ,3 ,4 union all
select 2 ,2 ,3 ,4
go
--a b c d
--1 2 3 4
--2 2 3 4
--3 3 6 6select *
from t1 t
where not exists (select 1 from t2 where a = t.a)
union all
select *
from t2
order by adrop table t1,t2
/**************a b c d
----------- ----------- ----------- -----------
1 2 3 4
2 2 3 4
3 3 6 6(3 行受影响)
select *
from t1 t
where not exists (select 1 from t2 where a = t.a)
union all
select *
from t2
order by adrop table t1,t2
create table t1(a int,b int,c int,d int)
insert into t1
select 1 ,1 ,2 ,2 union all
select 2 ,2 ,4 ,4 union all
select 3 ,3 ,6 ,6
gocreate table t2(a int,b int,c int,d int)
insert into t2
select 1 ,2 ,3 ,4 union all
select 2 ,2 ,3 ,4
go
--a b c d
--1 2 3 4
--2 2 3 4
--3 3 6 6insert into t2
select *
from t1 t
where not exists (select 1 from t2 where a = t.a)select *
from t2drop table t1,t2/************a b c d
----------- ----------- ----------- -----------
1 2 3 4
2 2 3 4
3 3 6 6(3 行受影响)
啥意思?
insert into t
select *
from t1 t
where not exists (select 1 from t2 where a = t.a)
union all
select *
from t2
order by a