第二个联接表的别名

时间:2018-10-13 07:21:29

标签: sql

我想知道如何为双联接表的组合创建别名。 因此,问题是TableOne自然地联接TableTwo,然后将TableThree与AID=AbID联接,我想给这个组合表一个别名,例如NewTable。 我尝试过:

((TableOne NATURAL JOIN TableTwo) JOIN TableThree ON (AID = AbID) AS NewTable

但似乎不起作用。

2 个答案:

答案 0 :(得分:0)

您可以(并且应该)为每个表添加别名:

 ((TableOne t1
   NATURAL JOIN TableTwo t2)
   JOIN TableThree t3
      ON (AID = AbID) AS NewTable

另请参见

SQL Table Alias

答案 1 :(得分:0)

首先,不要使用NATURAL JOIN。它确实被破坏了,因为它使用具有相同 name 的列,而不是正确声明的外键关系。这也使得代码真的很难调试。因此,请改用ONUSING

第二,您只能使用子查询来做您想做的事情:

FROM . . . 
     (SELECT . . .               -- fill in with the columns you want
      FROM TableOne t1 JOIN
           TableTwo t2
           USING ( . . . ) JOIN  -- fill in with the columns used for the `JOIN`
           TableThree t3
           ON ?.AID = ?.AbID     -- fill in with the table aliases where the columns come from
     ) NewTable
相关问题