postgresql 带有 AS 别名的不明确的列引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13003414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 00:27:33  来源:igfitidea点击:

Ambiguous Column Reference with an AS alias

sqlpostgresqlaliasambiguous

提问by Hyman

I am unsure as to how to resolve an ambiguous column reference when using an alias.

我不确定在使用别名时如何解决不明确的列引用。

Imagine two tables, aand bthat both have a namecolumn. If I join these two tables and alias the result, I do not know how to reference the namecolumn for both tables. I've tried out a few variants, but none of them work:

想象两个表,a并且b都有一name列。如果我加入这两个表并为结果设置别名,我不知道如何引用这name两个表的列。我尝试了一些变体,但它们都不起作用:

Attempt 1

尝试 1

SELECT a.name, b.name
FROM (a INNER JOIN b ON a.id = b.id) AS x

This doesn't work as aand bare out of scope.

这不起作用a并且b超出了范围。

Attempt 2

尝试 2

SELECT x.a.name, x.b.name
FROM (a INNER JOIN b ON a.id = b.id) AS x

SQL syntax doesn't work like that.

SQL 语法不是那样工作的。

Attempt 3

尝试 3

SELECT x.name, x.name
FROM (a INNER JOIN b ON a.id = b.id) AS x

That's just plain ambiguous!

这简直就是模棱两可!

I'm all out of ideas - any help would be much appreciated.

我完全没有想法 - 任何帮助将不胜感激。

回答by John Woo

don't enclose it with parenthesis since (a INNER JOIN b ON a.id = b.id)is not a complete query.

不要用括号括起来,因为(a INNER JOIN b ON a.id = b.id)它不是一个完整的查询。

SELECT  a.name AS A_Name, 
        b.name AS B_Name
FROM    a INNER JOIN b 
           ON a.id = b.id

or (assuming) if you have longer tables names and you want to make it short,

或者(假设)如果你有更长的表名并且你想缩短它,

SELECT  a.name AS A_Name, 
        b.name AS B_Name
FROM    longTableNameA a 
        INNER JOIN longTableNameB b 
           ON a.id = b.id