多表列合并

来源:互联网 发布:mac 音乐剪辑合成软件 编辑:程序博客网 时间:2024/04/29 03:15
 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 Bselect  * from Cselect  * 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 行受影响)

 

 

selectmax(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,Dunion selectmax(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 unionselectmax(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  unionselectmax(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,Dunionselectmax(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 行受影响)