简体   繁体   English

Postgres-引用完全限定的表或带标签的表联接后面的列

[英]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.

相关问题 数百个别名/同义词与数据库表的完全限定名称 - Hundreds of aliases/synonyms vs database tables' fully qualified names SqlPackage不接受同步触发器中表的标准名称 - SqlPackage not accepting fully qualified name of tables in sync trigger Postgres:一个包含许多列的表或几个包含较少列的表? - Postgres: one table with many columns or several tables with fewer columns? SQL Server:跟踪数据库活动或在完全限定的表查询上登录 - SQL Server : Trace Database activity or logins on fully qualified table query 在使用全限定名之前,表名是无效的? - Table name is invalid until I use the fully qualified name? 无法在动态SQL中使用标准表名 - Unable to use fully qualified table name in dynamic SQL 带有SP_ExecuteSql的标准表名可以访问远程服务器 - Fully qualified table names with SP_ExecuteSql to access remote server 我需要从表的主键引用的列和表 - I need columns and tables that taking reference from primary key of a table 获取所有表的所有列的列表以及由引用的表键所覆盖的引用表 - Get List of all columns of all tables and Reference table follwled by reference Key of table ssrs nullif完全限定的名称 - ssrs nullif fully qualified name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM