select s1,s2,s3 from a union all select s1,s2,'' from b
select s1,s2,s3 from a union all select s1,s2,null as s3 from b
select s1,s2,s3 from a union all select s1,s2,null from b
(select s1,s2,s3 from a) union (select s1,s2,null as s3 from b)
CREATE TABLE a ( s1 int, s2 int, s3 int) INSERT INTO a SELECT 3,4,6 UNION ALL SELECT 1,5,7 GOCREATE TABLE b ( s1 int, s2 int) INSERT INTO b SELECT 6,7 UNION ALL SELECT 7,7 GOSELECT a.s1,a.s2,a.s3 FROM a UNION ALL SELECT b.s1,b.s2,Null FROM b GODROP TABLE a GODROP TABLE b GO /* (2 個資料列受到影響)(2 個資料列受到影響) s1 s2 s3 ----------- ----------- ----------- 3 4 6 1 5 7 6 7 NULL 7 7 NULL(4 個資料列受到影響)*/
select s1,s2,s3 from a union all select s1,s2,null from b
if object_id('a表') is not null drop table a表 if object_id('b表') is not null drop table b表 gocreate table a表 ( s1 int, s2 int, s3 int ) insert into a表 select 3,4,6 union all select 1,5,7 create table b表 ( s1 int, s2 int ) insert into b表 select 6,7 union all select 7,7 go select * from a表 union all select s1,s2,null from b表 ---------------
select s1,s2,s3 from a union all select s1,s2,null from b
直接把两个表给连接起来就行了. select s1,s2,s3 from a union all select s1,s2,null from b
--建立测试环境 create table a(s1 varchar(10),s2 varchar(10),s3 varchar(10)) create table b(s1 varchar(10),s2 varchar(10)) insert into a select '3','4','6' union all select '1','5','7'insert into b select '6','7' union all select '7','7'--实现SQL语句 select * from a union all select s1,s2,null from b--删除测试环境 drop table a drop table b --结果(所影响的行数为 2 行) (所影响的行数为 2 行)s1 s2 s3 ---------- ---------- ---------- 3 4 6 1 5 7 6 7 NULL 7 7 NULL(所影响的行数为 4 行)
union all
select s1,s2,'' from b
union all
select s1,s2,null as s3 from b
union all
select s1,s2,null from b
union
(select s1,s2,null as s3 from b)
s1 int,
s2 int,
s3 int)
INSERT INTO a
SELECT 3,4,6 UNION ALL
SELECT 1,5,7
GOCREATE TABLE b (
s1 int,
s2 int)
INSERT INTO b
SELECT 6,7 UNION ALL
SELECT 7,7
GOSELECT a.s1,a.s2,a.s3 FROM a
UNION ALL
SELECT b.s1,b.s2,Null FROM b
GODROP TABLE a
GODROP TABLE b
GO
/*
(2 個資料列受到影響)(2 個資料列受到影響)
s1 s2 s3
----------- ----------- -----------
3 4 6
1 5 7
6 7 NULL
7 7 NULL(4 個資料列受到影響)*/
union all
select s1,s2,null from b
if object_id('a表') is not null
drop table a表
if object_id('b表') is not null
drop table b表
gocreate table a表
( s1 int,
s2 int,
s3 int
)
insert into a表 select 3,4,6
union all select 1,5,7
create table b表
( s1 int,
s2 int
)
insert into b表 select 6,7
union all select 7,7
go
select * from a表
union all
select s1,s2,null from b表 ---------------
union all
select s1,s2,null from b
select s1,s2,s3 from a
union all
select s1,s2,null from b
create table a(s1 varchar(10),s2 varchar(10),s3 varchar(10))
create table b(s1 varchar(10),s2 varchar(10))
insert into a
select '3','4','6'
union all
select '1','5','7'insert into b
select '6','7'
union all
select '7','7'--实现SQL语句
select * from a
union all
select s1,s2,null from b--删除测试环境
drop table a
drop table b
--结果(所影响的行数为 2 行)
(所影响的行数为 2 行)s1 s2 s3
---------- ---------- ----------
3 4 6
1 5 7
6 7 NULL
7 7 NULL(所影响的行数为 4 行)