postgresql Postgres 不会在列名之前接受表别名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11369757/
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:04:50  来源:igfitidea点击:

Postgres won't accept table alias before column name

sqlpostgresqljodd

提问by Ron Romero

I'm using a framework (Jodd) which is adding the table alias to the column names in a SQL Select. It looks like well-formed SQL, but Postgres chokes on it.

我正在使用一个框架 (Jodd),它将表别名添加到 SQL Select 中的列名。它看起来像格式良好的 SQL,但 Postgres 却卡住了它。

update GREETING Greeting 
     set Greeting.ID=5, 
         Greeting.NAME='World', 
         Greeting.PHRASE='Hello World!'  
where (Greeting.ID=5)

gives an error:

给出一个错误:

Error: ERROR: column "greeting" of relation "greeting" does not exist
SQLState:  42703

Is there a way to get Postgres to accept that SQL? My other alternative is to hack the framework, which I don't want to do.

有没有办法让 Postgres 接受那个 SQL?我的另一个选择是破解框架,我不想这样做。

回答by ypercube??

The problem is that you include the table alias in SETclause, in the columns. See the documentation of UPDATEin Postgres docs:

问题是您SET在列中包含了表别名 in子句。请参阅UPDATEPostgres 文档中的文档

column

The name of a column in table. The column name can be qualified with a subfield name or array subscript, if needed. Do not include the table's name in the specification of a target column — for example, UPDATE tab SET tab.col = 1is invalid.

column

中列的名称table。如果需要,可以使用子字段名称或数组下标来限定列名称。不要在目标列的规范中包括表的名称——例如,UPDATE tab SET tab.col = 1无效。

This is valid in Postgres:

这在 Postgres 中有效:

update GREETING Greeting 
set 
    NAME='World', 
    PHRASE='Hello World!' 
where Greeting.ID=5 ;

回答by vyegorov

Check documentation on UPDATEstatement, specifically for the columnpart: it is illegal to prefix columns with table alias in the SETclause.

检查UPDATE语句的文档,特别是部分:在SET子句中使用表别名前缀列是非法的。

UPDATE GREETING Greeting
   SET ID=5, NAME='World', PHRASE='Hello World!'
 WHERE (Greeting.ID=5);

回答by igr

Try using the latest Jodd, v3.3.7. where this issue is fixed.

尝试使用最新的 Jodd v3.3.7。修复此问题的地方。

The problem was in the Jodd library: entity update methods were generating update statement with table aliases. The new version simply does not put table aliases; that works for Postgres and for other databases too.

问题出在 Jodd 库中:实体更新方法正在生成带有表别名的更新语句。新版本根本就不放表别名;这也适用于 Postgres 和其他数据库。