[英]Postgres - reference fully qualified tables or columns behind labeled table joines
What's the correct method or syntax for referencing a fully qualified table/column when a group of table joins has been labeled? 标记一组表联接后,引用完全限定的表/列的正确方法或语法是什么? Context: 语境:
SELECT ... FROM
(A JOIN B ON A.?=B.? JOIN C on B.?=C.?) AS ABCs
LEFT OUTER JOIN
(X JOIN W on X.?=W.? JOIN Z on W.?=Z.?) AS XYZs on ABCs.?=XYZ.?
Seemingly when labeling a table, or group of tables you can no longer reference obfuscated tables directly - you must use the label name. 似乎在标记一个表或一组表时,您不再可以直接引用混淆的表-您必须使用标签名。 For example, in the above code each table has an ID column, which you'd normally reference via A.id or B.id, however when labeled the tables you must reference via ABCs.id - though, this obviously causes the '... is ambiguous' error. 例如,在上面的代码中,每个表都有一个ID列,通常您会通过A.id或B.id来引用该ID列,但是,在标记了这些表时,您必须通过ABCs.id进行引用-尽管,这显然会导致'。 ..是模棱两可的”错误。
I was hoping for a solution along similar lines to "ABCs.A.id". 我希望以与“ ABCs.A.id”类似的方式寻求解决方案。
FWIW, in practice I'm using this to join a large composite table on to itself, hence the naming convention. FWIW,在实践中,我使用它来将大型组合表连接到自身,因此使用了命名约定。
In your query, the FROM clause is evaluated first. 在您的查询中,首先评估FROM子句。 That has the effect of hiding table "a" from the rest of the query. 这样做的结果是对其余查询隐藏表“ a”。 (The same rule applies to column aliases, too.) (相同的规则也适用于列别名。)
I threw together some nonsense tables for demonstration. 我把一些废话放在一起作示范。 This query works. 该查询有效。
SELECT *
FROM
(A JOIN B ON A.a_id =B.b_id
JOIN C on B.b_id=C.c_id) AS ABCs
LEFT OUTER JOIN
(X JOIN W on X.x_id=W.w_id
JOIN Z on W.w_id=Z.z_id) AS XYZs on ABCs.a_id=XYZs.w_id
But this one, which refers to A.a_id in the SELECT clause, does not. 但是,这在SELECT子句中引用A.a_id,却没有。
SELECT A.a_id, *
FROM
(A JOIN B ON A.a_id =B.b_id
JOIN C on B.b_id=C.c_id) AS ABCs
LEFT OUTER JOIN
(X JOIN W on X.x_id=W.w_id
JOIN Z on W.w_id=Z.z_id) AS XYZs on ABCs.a_id=XYZs.w_id
ERROR: invalid reference to FROM-clause entry for table "a"
LINE 1: SELECT A.a_id, *
^
HINT: There is an entry for table "a", but it cannot be referenced from
this part of the query.
Refer to it by the alias you gave to the join expression instead. 通过为连接表达式指定的别名来引用它。
SELECT ABCs.a_id, *
FROM
(A JOIN B ON A.a_id =B.b_id
JOIN C on B.b_id=C.c_id) AS ABCs
LEFT OUTER JOIN
(X JOIN W on X.x_id=W.w_id
JOIN Z on W.w_id=Z.z_id) AS XYZs on ABCs.a_id=XYZs.w_id
To use the same join expression twice, use two different aliases. 要两次使用相同的联接表达式,请使用两个不同的别名。
SELECT *
FROM
(A JOIN B ON A.a_id =B.b_id
JOIN C on B.b_id=C.c_id) AS ABC_1s
LEFT OUTER JOIN
(A JOIN B ON A.a_id =B.b_id
JOIN C on B.b_id=C.c_id) AS ABC_2s on ABC_1s.a_id=ABC_2s.c_id
Use the same syntax, alias_name.column_name, in the SELECT clause if you need to. 如果需要,请在SELECT子句中使用相同的语法alias_name.column_name。
SELECT ABC_1s.a_id, ABC_2s.a_id, *
FROM
(A JOIN B ON A.a_id =B.b_id
JOIN C on B.b_id=C.c_id) AS ABC_1s
LEFT OUTER JOIN
(A JOIN B ON A.a_id =B.b_id
JOIN C on B.b_id=C.c_id) AS ABC_2s on ABC_1s.a_id=ABC_2s.c_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.