从WITH-Subselect引用列

时间:2014-04-16 14:19:49

标签: sql oracle subquery with-clause

问题如下: 我使用子查询,我需要加入

WITH subselect_from_A AS (
    SELECT A.A_ID FROM A
)
SELECT B.D_ID FROM B, subselect_from_A WHERE B.B_ID = 
A.A_ID
/* or instead: subselect_from_A.A_ID */

如何从子选择中引用该列?

2 个答案:

答案 0 :(得分:2)

首先,您应该停止使用已弃用的隐式联接并改为使用显式联接。然后你可以使用表别名:

WITH subselect_from_A AS (
    SELECT A.A_ID FROM A --doesn't really seem like a sub-select
)
SELECT B.D_ID, A.A_ID
FROM B
INNER JOIN subselect_from_A AS A
    ON B.B_ID = A.A_ID;

答案 1 :(得分:1)

与常规表格相同。

WITH subselect_from_A AS (
    SELECT A.A_ID FROM A
)
SELECT B.D_ID, subselect_from_A.A_ID FROM B, subselect_from_A
WHERE B.B_ID = subselect_from_A.A_ID;