列名 A B C D E F G H I J K M
1 a b 2 c d 3 e f 4 g h
要通过一条SQL语句得到如下效果(不能用存储过程,只能一条SQL语句):
1 a b
2 c d
3 e f
4 g h
18 个解决方案
#1
select a,b,c union all
select d,e,f union all
select g,h,i union all
select j,k,m
select d,e,f union all
select g,h,i union all
select j,k,m
#2
select a,b,c union select d,e,f union select g,h,i union select j,k,m
#3
select a,b,c
union select d,e,f
union select g,h,i
union select j,k,m
from table
union select d,e,f
union select g,h,i
union select j,k,m
from table
#4
select a,b,c from tab union all
select d,e,f from tab union all
select g,h,i from tab union all
select j,k,m from tab
select d,e,f from tab union all
select g,h,i from tab union all
select j,k,m from tab
#5
晕,光急着抢呢,连格式都错了
#6
select a,b,c from tablename union all
select d,e,f from tablename union all
select g,h,i from tablename union all
select j,k,m
select d,e,f from tablename union all
select g,h,i from tablename union all
select j,k,m
#7
select a,b,c from tablename union all
select d,e,f from tablename union all
select g,h,i from tablename union all
select j,k,m
select d,e,f from tablename union all
select g,h,i from tablename union all
select j,k,m
#8
select a,b,c from tablename union all
select d,e,f from tablename union all
select g,h,i from tablename union all
select j,k,m
select d,e,f from tablename union all
select g,h,i from tablename union all
select j,k,m
#9
暈,怎麼發了兩個
#10
create table test(A char(1), B char(1),C char(1),D char(1),E char(1),F char(1),G char(1),H char(1),I char(1),J char(1),K char(1),M char(1))
go
insert test select '1','a','b','2','c','d','3','e','f','4','g','h'
go
select * from test;
go
select a,b,c from test
union select d,e,f from test
union select g,h,i from test
union select j,k,m from test
A B C D E F G H I J K M
---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
1 a b 2 c d 3 e f 4 g h
(所影响的行数为 1 行)
a b c
---- ---- ----
1 a b
2 c d
3 e f
4 g h
(所影响的行数为 4 行)
go
insert test select '1','a','b','2','c','d','3','e','f','4','g','h'
go
select * from test;
go
select a,b,c from test
union select d,e,f from test
union select g,h,i from test
union select j,k,m from test
A B C D E F G H I J K M
---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
1 a b 2 c d 3 e f 4 g h
(所影响的行数为 1 行)
a b c
---- ---- ----
1 a b
2 c d
3 e f
4 g h
(所影响的行数为 4 行)
#11
--哈,也更正一下吧
select a,b,c from TAB union all
select d,e,f from TAB union all
select g,h,i from TAB union all
select j,k,m
select a,b,c from TAB union all
select d,e,f from TAB union all
select g,h,i from TAB union all
select j,k,m
#12
select a,b,c from tablename union all
select d,e,f from tablename union all
select g,h,i from tablename union all
select j,k,m from tablename
select d,e,f from tablename union all
select g,h,i from tablename union all
select j,k,m from tablename
#13
我可能说得不够明白啊,因为我那个表
A B C D E F G H I J K M
---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
1 a b 2 c d 3 e f 4 g h
是通过很长的语句得来,而且那条语句里面有where 条件的,如不是这样我也不来问大家了,除了union 还有别的写法吗?
A B C D E F G H I J K M
---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
1 a b 2 c d 3 e f 4 g h
是通过很长的语句得来,而且那条语句里面有where 条件的,如不是这样我也不来问大家了,除了union 还有别的写法吗?
#14
---错了更正
select a,b,c from tablename union all
select d,e,f from tablename union all
select g,h,i from tablename union all
select j,k,m from tablename
select a,b,c from tablename union all
select d,e,f from tablename union all
select g,h,i from tablename union all
select j,k,m from tablename
#15
---创建测试环境
Declare @T Table(A int,B varchar(8),C varchar(8),
D int,E varchar(8),F varchar(8),
G int,H varchar(8),I varchar(8),
J int,K varchar(8),M varchar(8))
Insert @T Select 1,'a','b',2,'c','d',3,'e','f',4,'g','h'
Union All Select 5,'x','x',6,'y','y',7,'z','z',7,'t','t'
Select * From @T
---查询结果
select a,b,c from @T union all
select d,e,f from @T union all
select g,h,i from @T union all
select j,k,m from @T
--结果
/*
a b c
----------- -------- --------
1 a b
5 x x
2 c d
6 y y
3 e f
7 z z
4 g h
7 t t
(所影响的行数为 8 行)
*/
Declare @T Table(A int,B varchar(8),C varchar(8),
D int,E varchar(8),F varchar(8),
G int,H varchar(8),I varchar(8),
J int,K varchar(8),M varchar(8))
Insert @T Select 1,'a','b',2,'c','d',3,'e','f',4,'g','h'
Union All Select 5,'x','x',6,'y','y',7,'z','z',7,'t','t'
Select * From @T
---查询结果
select a,b,c from @T union all
select d,e,f from @T union all
select g,h,i from @T union all
select j,k,m from @T
--结果
/*
a b c
----------- -------- --------
1 a b
5 x x
2 c d
6 y y
3 e f
7 z z
4 g h
7 t t
(所影响的行数为 8 行)
*/
#16
---加个排序的
---创建测试环境
Declare @T Table(A int,B varchar(8),C varchar(8),
D int,E varchar(8),F varchar(8),
G int,H varchar(8),I varchar(8),
J int,K varchar(8),M varchar(8))
Insert @T Select 1,'a','b',2,'c','d',3,'e','f',4,'g','h'
Union All Select 5,'x','x',6,'y','y',7,'z','z',8,'t','t'
Select * From @T
---查询结果
select * from (
select a,b,c from @T union all
select d,e,f from @T union all
select g,h,i from @T union all
select j,k,m from @T
) T order by a
--结果
/*
a b c
----------- -------- --------
1 a b
2 c d
3 e f
4 g h
5 x x
6 y y
7 z z
8 t t
(所影响的行数为 8 行)
*/
---创建测试环境
Declare @T Table(A int,B varchar(8),C varchar(8),
D int,E varchar(8),F varchar(8),
G int,H varchar(8),I varchar(8),
J int,K varchar(8),M varchar(8))
Insert @T Select 1,'a','b',2,'c','d',3,'e','f',4,'g','h'
Union All Select 5,'x','x',6,'y','y',7,'z','z',8,'t','t'
Select * From @T
---查询结果
select * from (
select a,b,c from @T union all
select d,e,f from @T union all
select g,h,i from @T union all
select j,k,m from @T
) T order by a
--结果
/*
a b c
----------- -------- --------
1 a b
2 c d
3 e f
4 g h
5 x x
6 y y
7 z z
8 t t
(所影响的行数为 8 行)
*/
#17
长知识
#18
我可能说得不够明白啊,因为我那个表
A B C D E F G H I J K M
---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
1 a b 2 c d 3 e f 4 g h
是通过很长的语句得来,而且那条语句里面有where 条件的,如不是这样我也不来问大家了,除了union 还有别的写法吗?
----------------------------------------------------
那就再动态构造 SELECT...UNION ALL...的语句,除了这个方法,似乎没别的方法了!
A B C D E F G H I J K M
---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
1 a b 2 c d 3 e f 4 g h
是通过很长的语句得来,而且那条语句里面有where 条件的,如不是这样我也不来问大家了,除了union 还有别的写法吗?
----------------------------------------------------
那就再动态构造 SELECT...UNION ALL...的语句,除了这个方法,似乎没别的方法了!
#1
select a,b,c union all
select d,e,f union all
select g,h,i union all
select j,k,m
select d,e,f union all
select g,h,i union all
select j,k,m
#2
select a,b,c union select d,e,f union select g,h,i union select j,k,m
#3
select a,b,c
union select d,e,f
union select g,h,i
union select j,k,m
from table
union select d,e,f
union select g,h,i
union select j,k,m
from table
#4
select a,b,c from tab union all
select d,e,f from tab union all
select g,h,i from tab union all
select j,k,m from tab
select d,e,f from tab union all
select g,h,i from tab union all
select j,k,m from tab
#5
晕,光急着抢呢,连格式都错了
#6
select a,b,c from tablename union all
select d,e,f from tablename union all
select g,h,i from tablename union all
select j,k,m
select d,e,f from tablename union all
select g,h,i from tablename union all
select j,k,m
#7
select a,b,c from tablename union all
select d,e,f from tablename union all
select g,h,i from tablename union all
select j,k,m
select d,e,f from tablename union all
select g,h,i from tablename union all
select j,k,m
#8
select a,b,c from tablename union all
select d,e,f from tablename union all
select g,h,i from tablename union all
select j,k,m
select d,e,f from tablename union all
select g,h,i from tablename union all
select j,k,m
#9
暈,怎麼發了兩個
#10
create table test(A char(1), B char(1),C char(1),D char(1),E char(1),F char(1),G char(1),H char(1),I char(1),J char(1),K char(1),M char(1))
go
insert test select '1','a','b','2','c','d','3','e','f','4','g','h'
go
select * from test;
go
select a,b,c from test
union select d,e,f from test
union select g,h,i from test
union select j,k,m from test
A B C D E F G H I J K M
---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
1 a b 2 c d 3 e f 4 g h
(所影响的行数为 1 行)
a b c
---- ---- ----
1 a b
2 c d
3 e f
4 g h
(所影响的行数为 4 行)
go
insert test select '1','a','b','2','c','d','3','e','f','4','g','h'
go
select * from test;
go
select a,b,c from test
union select d,e,f from test
union select g,h,i from test
union select j,k,m from test
A B C D E F G H I J K M
---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
1 a b 2 c d 3 e f 4 g h
(所影响的行数为 1 行)
a b c
---- ---- ----
1 a b
2 c d
3 e f
4 g h
(所影响的行数为 4 行)
#11
--哈,也更正一下吧
select a,b,c from TAB union all
select d,e,f from TAB union all
select g,h,i from TAB union all
select j,k,m
select a,b,c from TAB union all
select d,e,f from TAB union all
select g,h,i from TAB union all
select j,k,m
#12
select a,b,c from tablename union all
select d,e,f from tablename union all
select g,h,i from tablename union all
select j,k,m from tablename
select d,e,f from tablename union all
select g,h,i from tablename union all
select j,k,m from tablename
#13
我可能说得不够明白啊,因为我那个表
A B C D E F G H I J K M
---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
1 a b 2 c d 3 e f 4 g h
是通过很长的语句得来,而且那条语句里面有where 条件的,如不是这样我也不来问大家了,除了union 还有别的写法吗?
A B C D E F G H I J K M
---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
1 a b 2 c d 3 e f 4 g h
是通过很长的语句得来,而且那条语句里面有where 条件的,如不是这样我也不来问大家了,除了union 还有别的写法吗?
#14
---错了更正
select a,b,c from tablename union all
select d,e,f from tablename union all
select g,h,i from tablename union all
select j,k,m from tablename
select a,b,c from tablename union all
select d,e,f from tablename union all
select g,h,i from tablename union all
select j,k,m from tablename
#15
---创建测试环境
Declare @T Table(A int,B varchar(8),C varchar(8),
D int,E varchar(8),F varchar(8),
G int,H varchar(8),I varchar(8),
J int,K varchar(8),M varchar(8))
Insert @T Select 1,'a','b',2,'c','d',3,'e','f',4,'g','h'
Union All Select 5,'x','x',6,'y','y',7,'z','z',7,'t','t'
Select * From @T
---查询结果
select a,b,c from @T union all
select d,e,f from @T union all
select g,h,i from @T union all
select j,k,m from @T
--结果
/*
a b c
----------- -------- --------
1 a b
5 x x
2 c d
6 y y
3 e f
7 z z
4 g h
7 t t
(所影响的行数为 8 行)
*/
Declare @T Table(A int,B varchar(8),C varchar(8),
D int,E varchar(8),F varchar(8),
G int,H varchar(8),I varchar(8),
J int,K varchar(8),M varchar(8))
Insert @T Select 1,'a','b',2,'c','d',3,'e','f',4,'g','h'
Union All Select 5,'x','x',6,'y','y',7,'z','z',7,'t','t'
Select * From @T
---查询结果
select a,b,c from @T union all
select d,e,f from @T union all
select g,h,i from @T union all
select j,k,m from @T
--结果
/*
a b c
----------- -------- --------
1 a b
5 x x
2 c d
6 y y
3 e f
7 z z
4 g h
7 t t
(所影响的行数为 8 行)
*/
#16
---加个排序的
---创建测试环境
Declare @T Table(A int,B varchar(8),C varchar(8),
D int,E varchar(8),F varchar(8),
G int,H varchar(8),I varchar(8),
J int,K varchar(8),M varchar(8))
Insert @T Select 1,'a','b',2,'c','d',3,'e','f',4,'g','h'
Union All Select 5,'x','x',6,'y','y',7,'z','z',8,'t','t'
Select * From @T
---查询结果
select * from (
select a,b,c from @T union all
select d,e,f from @T union all
select g,h,i from @T union all
select j,k,m from @T
) T order by a
--结果
/*
a b c
----------- -------- --------
1 a b
2 c d
3 e f
4 g h
5 x x
6 y y
7 z z
8 t t
(所影响的行数为 8 行)
*/
---创建测试环境
Declare @T Table(A int,B varchar(8),C varchar(8),
D int,E varchar(8),F varchar(8),
G int,H varchar(8),I varchar(8),
J int,K varchar(8),M varchar(8))
Insert @T Select 1,'a','b',2,'c','d',3,'e','f',4,'g','h'
Union All Select 5,'x','x',6,'y','y',7,'z','z',8,'t','t'
Select * From @T
---查询结果
select * from (
select a,b,c from @T union all
select d,e,f from @T union all
select g,h,i from @T union all
select j,k,m from @T
) T order by a
--结果
/*
a b c
----------- -------- --------
1 a b
2 c d
3 e f
4 g h
5 x x
6 y y
7 z z
8 t t
(所影响的行数为 8 行)
*/
#17
长知识
#18
我可能说得不够明白啊,因为我那个表
A B C D E F G H I J K M
---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
1 a b 2 c d 3 e f 4 g h
是通过很长的语句得来,而且那条语句里面有where 条件的,如不是这样我也不来问大家了,除了union 还有别的写法吗?
----------------------------------------------------
那就再动态构造 SELECT...UNION ALL...的语句,除了这个方法,似乎没别的方法了!
A B C D E F G H I J K M
---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
1 a b 2 c d 3 e f 4 g h
是通过很长的语句得来,而且那条语句里面有where 条件的,如不是这样我也不来问大家了,除了union 还有别的写法吗?
----------------------------------------------------
那就再动态构造 SELECT...UNION ALL...的语句,除了这个方法,似乎没别的方法了!