SQL - 派生表之间的共享字段

时间:2017-01-04 17:18:15

标签: sql derived-table

是否可以跨派生表访问字段?

SELECT * 
FROM   (SELECT ID, COL1A FROM Table1) T1
       JOIN (SELECT ID, COL2A FROM Table2) T2
           ON T1.ID = T2.ID
       JOIN (SELECT ID, (COL3A + T2.COL2A) AS SUM FROM Table3) T3
           ON T1.ID = T3.ID

1 个答案:

答案 0 :(得分:2)

您可以在SELECT子句中使用多个列来表达表达式:

SELECT t1.ID, t1.COL1A, t2.COL2A, (t3.COL3A + t2.COL2A) as sum
FROM Table1 T1 JOIN
     Table2 T2
     ON T1.ID = T2.ID JOIN
     Table3 T3
     ON T1.ID = T3.ID;

根本不需要派生表。