将一行转换为 PostgreSQL 中的数组

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

Convert one row to array in PostgreSQL

sqlarrayspostgresqlrow

提问by Le Ngoc Loan

I have table:

我有表:

Id Code Name
1  001  Marry

I want this result:

我想要这个结果:

{1,001,Marry}

回答by Erwin Brandstetter

Use an array constructor:

使用数组构造函数

SELECT ARRAY[Id::text, Code::text, Name::text] AS my_arr FROM tbl;

We need a common type, of course. Array element have to be of the same type. Every type can be cast to text, so it's the obvious choice here.

当然,我们需要一个通用类型。数组元素必须是相同的类型。每种类型都可以强制转换为text,因此这里是显而易见的选择。