在PostgreSQL中使用SELECT查询联接列 [英] Join a Column with SELECT query in PostgreSQL
问题描述
在此MySQL 查询上,需要在PostgreSQL上获得相同的结果。
Need this equivalent outcome on PostgreSQL on this MySQL query.
select id, 'ID1' from supportContacts
想法是将表名称和行值等于ID1的列连接起来。
Idea is to Join a Column with table name and row values equals to ID1.
在PostgreSQL上执行相同的查询会提供所需的行值,但给出?列?
Executing the same query on PostgreSQL gives the desired row values but gives ?column? uknown
as column name.
哪个PostgreSQL查询将给出确切的输出?
Which PostgreSQL query will give the exact output?
推荐答案
添加列别名来分配您选择的名称。其他系统将应用默认设置。
要分配类型,请使用显式的 cast 。我在这里强制转换为文本
:
Add a column alias to assign a name of your choosing. Else the system applies defaults.
To assign a type, use an explicit cast. I cast to text
here:
SELECT id, 'ID1'::text AS "ID1" FROM supportContacts
或使用SQL标准 cast()
以使其可移植:
Or use the SQL standard cast()
to make it portable:
SELECT id, cast('ID1' AS varchar) AS "ID1" FROM "supportContacts"
对于可移植性,请确保MySQL与 SET sql_mode ='ANSI'
。
For portability, make sure that MySQL runs with SET sql_mode = 'ANSI'
.
在PostgreSQL中,未引用的CaMeL大小写名称(例如 supportContacts
)也被强制转换为小写。使用 supportContacts
或 supportcontacts
取决于实际的表名。
Also, unquoted CaMeL-case names like supportContacts
are cast to lower case in PostgreSQL. Use "supportContacts"
or supportcontacts
depending on the actual table name.
首先阅读优秀的手册这里了解有关标识符的基础知识。
Start by reading the excellent manual here for basics about identifiers.
这篇关于在PostgreSQL中使用SELECT查询联接列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!