我可以在选择列表中使用Postgres关键字作为别名吗? [英] Can I use a Postgres keyword as an alias in select list?

查看:198
本文介绍了我可以在选择列表中使用Postgres关键字作为别名吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天早上我遇到了一个奇怪的问题。我正在创建一个视图,简化了Postgres表中的应用程序列表。

I encountered a strange issue this morning. I was creating a view simplifying a list of applications in a Postgres table.

此操作失败。

CREATE OR REPLACE VIEW application_view AS 
SELECT COALESCE( nullif(full_name,''), nullif(additional_info,''), app_name) name
, id
FROM application
ORDER BY COALESCE( nullif(full_name,''), nullif(additional_info,''), app_name) 

CREATE OR REPLACE VIEW application_view AS 
SELECT COALESCE( nullif(full_name,''), nullif(additional_info,''), app_name) application
, id
FROM application
ORDER BY COALESCE( nullif(full_name,''), nullif(additional_info,''), app_name) 

有效。

我经常使用名称作为表中的列名称,所以关于为什么第一个sql语句失败的任何想法?

I often use name as a column name in tables so any ideas as to why the first sql statement failed?

推荐答案

这是关键字。如果要在选择列表中使用关键字作为别名,则必须使用单词 as

It's a keyword. When you want to use a keyword as an alias in the select list you have to use the word as:

select 1 name;

ERROR:  syntax error at or near "name"
LINE 1: select 1 name;

select 1 as name;

 name 
------
    1
(1 row)

来自文档关于选择列表中的别名:

From the documentation about aliases in the select list:


要指定用于输出列的名称,请写AS output_name 。 (您可以省略AS,但仅在所需的输出名称与任何PostgreSQL关键字都不匹配时才可以(请参阅附录C)。为了防止将来可能再添加关键字,建议始终编写AS或对输出名称加双引号。 )

To specify the name to use for an output column, write AS output_name after the column's expression. (You can omit AS, but only if the desired output name does not match any PostgreSQL keyword (see Appendix C). For protection against possible future keyword additions, it is recommended that you always either write AS or double-quote the output name.)

这篇关于我可以在选择列表中使用Postgres关键字作为别名吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆