多表列合并

 
create table A(a varchar(50))
create table B(b varchar(50))
create table C(c varchar(50))
create table D(d varchar(50),e varchar(50) )

 
INSERT INTO A VALUES('a1')
 INSERT INTO A VALUES('a2')
 INSERT INTO A VALUES('a3')
 INSERT INTO A VALUES('a4')
  INSERT INTO A VALUES('a5')
  
INSERT INTO B VALUES('b1')
INSERT INTO B VALUES('b2')
INSERT INTO B VALUES('b3')
INSERT INTO B VALUES('b4')
INSERT INTO B VALUES('b5')


INSERT INTO C VALUES('c1')
INSERT INTO C VALUES('c2')
INSERT INTO C VALUES('c3')
INSERT INTO C VALUES('c4')
INSERT INTO C VALUES('c5')
  
  INSERT INTO D VALUES('d1','e1')
  
  
  INSERT INTO D VALUES('d2','e2')
  INSERT INTO D VALUES('d3','e3')
  INSERT INTO D VALUES('d4','e4')
  INSERT INTO D VALUES('d5','e5')
  
 
select  * from A     
select  * from B
select  * from C
select  * from D

a
--------------------------------------------------
a1
a2
a3
a4
a5

(5 行受影响)

b
--------------------------------------------------
b1
b2
b3
b4
b5

(5 行受影响)

c
--------------------------------------------------
c1
c2
c3
c4
c5

(5 行受影响)

d                                                  e
-------------------------------------------------- --------------------------------------------------
d1                                                 e1
d2                                                 e2
d3                                                 e3
d4                                                 e4
d5                                                 e5

(5 行受影响)

 

 

select
max(case a when 'a1' then a else '' end )as 'one',
max(case b when 'b1' then b else '' end )as 'two',
max(case c when 'c1' then c else '' end )as 'thre' ,
max(case d when 'd1' then d else '' end )as 'four' ,
max(case e when 'e1' then e else '' end )as 'six' 
from A,B,C,D


union 
select
max(case a when 'a2' then a else '' end )as 'one',
max(case b when 'b2' then b else '' end )as 'two',
max(case c when 'c2' then c else '' end )as 'thre' ,
max(case d when 'd2' then d else '' end )as 'four' ,
max(case e when 'e2' then e else '' end )as 'six' 
from A,B,C,D union
select
max(case a when 'a3' then a else '' end )as 'one',
max(case b when 'b3' then b else '' end )as 'two',
max(case c when 'c3' then c else '' end )as 'thre' ,
max(case d when 'd3' then d else '' end )as 'four' ,
max(case e when 'e3' then e else '' end )as 'six' 
from A,B,C,D  union
select
max(case a when 'a4' then a else '' end )as 'one',
max(case b when 'b4' then b else '' end )as 'two',
max(case c when 'c4' then c else '' end )as 'thre' ,
max(case d when 'd4' then d else '' end )as 'four' ,
max(case e when 'e4' then e else '' end )as 'six' 
from A,B,C,D
union
select
max(case a when 'a5' then a else '' end )as 'one',
max(case b when 'b5' then b else '' end )as 'two',
max(case c when 'c5' then c else '' end )as 'thre' ,
max(case d when 'd5' then d else '' end )as 'four' ,
max(case e when 'e5' then e else '' end )as 'six' 

from A,B,C,D


  
           
  
  
 

one                                                two                                                thre                                               four                                               six
-------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- --------------------------------------------------
a1                                                 b1                                                 c1                                                 d1                                                 e1
a2                                                 b2                                                 c2                                                 d2                                                 e2
a3                                                 b3                                                 c3                                                 d3                                                 e3
a4                                                 b4                                                 c4                                                 d4                                                 e4
a5                                                 b5                                                 c5                                                 d5                                                 e5

(5 行受影响)

 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值