sql,对重命名的列使用联接

sql, using join with renamed columns

我有两个具有相似名称列的表,我想对它们进行连接。所以我必须重命名列

表 1

|--------------------|------------------|
|      col1           |     col2         |
|--------------------|------------------|
|          1         |         5       |
|--------------------|------------------|
|          2         |         6       |
|--------------------|------------------|
|          3         |         7       |
|--------------------|------------------|
|          4         |         8       |

表 2

|--------------------|------------------|
|      col3          |     col2         |
|--------------------|------------------|
|          10        |         5       |
|--------------------|------------------|
|          11        |         6       |
|--------------------|------------------|
|          12        |         7       |
|--------------------|------------------|
|          13        |         8       |

我想要的

|--------------------|------------------|------------------|
|      col1          |     col2         |     col3         |
|--------------------|------------------|
|          1         |         5       |         10       |
|--------------------|------------------|------------------|
|          2         |         6       |         11       |
|--------------------|------------------|------------------|
|          3         |         7       |         12       |
|--------------------|------------------|------------------|
|          4         |         8       |         13       |

但我似乎无法弄清楚如何正确排序代码

select col1 as T1_col1,
      col2 as T1_col2
from Table1 as T1
inner join 
    (select col3 as T2_col3,
        col2 as T2_col2
     from Table2 as T2)
    on T1.T1_col2 = T2.T2_col2

执行此操作的正确方法是什么?

也许我误解了你的问题,但我相信你只需要在括号外使用第二个 table 别名,其他一切对我来说似乎都很好。

select T1.col1 as T1_col1,
  T2.col2 as T2_col2 --assuming you want col2 from table 2, otherwise your join would make no sense...
from Table1 as T1
inner join 
(select col3 as T2_col3,
    col2 as T2_col2
 from Table2)T2
on T1.col2 = T2.col2

无论如何您都不需要列别名。您可以改用 table 别名并在整个查询过程中使用它。

select T1.col1,
       T1.col2,
       T2.col3
from  Table1 as T1
inner join 
   Table2 as T2
   on T1.col2 = T2.col2
order by T1.col1, 
         T1.col2;

语法需要更正,我已经使用并编辑了您自己的查询以获得所需的结果:

WITH Table1 AS(
SELECT 1 COL1, 5 COL2 FROM DUAL
UNION 
SELECT 2     , 6  FROM DUAL
UNION 
SELECT 3     , 7  FROM DUAL
UNION 
SELECT 4     , 8  FROM DUAL)
, Table2 AS(
SELECT 10 COL3, 5 COL2 FROM DUAL
UNION 
SELECT 11     , 6 FROM DUAL
UNION 
SELECT 12     , 7 FROM DUAL
UNION 
SELECT 13     , 8 FROM DUAL
)
/*
SELECT
COL1, TAB2.COL2, COL3
FROM TAB1
JOIN TAB2
ON TAB1.COL2 = TAB2.COL2;
*/
Select col1 as T1_col1,
      col2 as T1_col2,
      T2_COL3
from Table1 T1
inner join 
    (select col3 as T2_col3,
        col2 as T2_col2
     from Table2 )T2
    on T1.col2 = T2.T2_col2;