使用select语句和别名连接多个表

时间:2018-06-15 08:31:34

标签: sql join stored-procedures db2 left-join

我正在进行db2存储过程,我将多个表连接到一个新表中,并将tablename作为前缀添加到所有列名中。遇到很多障碍,我可以使用一些帮助来构建这个陈述。请参阅我在下面尝试做的示例。有人有关于如何做到这一点的建议吗?

CREATE TABLE <new_table> AS 
SELECT * FROM (
    SELECT t1.<column1> AS t1.<table1>_<column1>, t1.<column2> AS t1.<table1>_<column2> , ... FROM <table1> t1
    LEFT JOIN 
    (SELECT <column1> AS <table2>_<column1>, <column2> AS <table2>_<column2> , ... FROM <table2>) t2
    ON t2.<table2>_<column2> = t1.<table1>_<column1>
    LEFT JOIN 
    (SELECT <column1> AS <table3>_<column1>, <column2> AS <table3>_<column2> , ... FROM <table3>) t3
    ON t2.<table2>_<column1> = t3.<table3>_<column1>
);

1 个答案:

答案 0 :(得分:0)

当列名包含句点或特殊字符时,在SQL或DDL中使用列名时,始终需要双引号。这有时很痛苦。

以下示例适用于Db2-LUW V11.1.3.3或更高版本:

create table thom.o1 (a integer, b integer);
create table thom.o2 (a integer, b integer);
create table thom.o3 (a integer, b integer);

insert into thom.o1(a,b) values(1,2);
insert into thom.o2(a,b) values(1,4),(1,3),(1,5),(2,9);
insert into thom.o3(a,b) values(1,6),(1,7),(1,8),(3,10);

create table thom.new_tab as (
   select t1.a as "t1.o1.a"
         ,t1.b as "t1.o1.b"
         ,t2.a as "t2.o2.a"
         ,t2.b as "t2.o2.b"
         ,t3.a as "t3.o3.a"
         ,t3.b as "t3.o3.b"
   from thom.o1 t1
   left join thom.o2 t2
   on t1.a = t2.a
   left join thom.o3 t3
   on t2.a = t3.a
) with data
;

描述表thom.new_tab;

                                Data type                     Column
Column name                     schema    Data type name      Length     Scale Nulls
------------------------------- --------- ------------------- ---------- ----- ------
t1.o1.a                         SYSIBM    INTEGER                      4     0 Yes   
t1.o1.b                         SYSIBM    INTEGER                      4     0 Yes   
t2.o2.a                         SYSIBM    INTEGER                      4     0 Yes   
t2.o2.b                         SYSIBM    INTEGER                      4     0 Yes   
t3.o3.a                         SYSIBM    INTEGER                      4     0 Yes   
t3.o3.b                         SYSIBM    INTEGER                      4     0 Yes   

  6 record(s) selected.

从thom.new_tab中选择*;

select * from thom.new_tab

t1.o1.a     t1.o1.b     t2.o2.a     t2.o2.b     t3.o3.a     t3.o3.b    
----------- ----------- ----------- ----------- ----------- -----------
          1           2           1           4           1           6
          1           2           1           5           1           6
          1           2           1           3           1           6
          1           2           1           4           1           7
          1           2           1           5           1           7
          1           2           1           3           1           7
          1           2           1           4           1           8
          1           2           1           5           1           8
          1           2           1           3           1           8

  9 record(s) selected.