postgresql 转换数组类型

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

Conversion array types

postgresqlpostgresql-9.2

提问by Oto Shavadze

I have in table column, which type is CHARACTER VARYING[](that is array)

我在表列中,哪种类型是CHARACTER VARYING[](即数组)

I need concatenate existed rows whith other array

我需要连接其他数组的现有行

This is my code:

这是我的代码:

UPDATE my_table SET
col = array_cat(col, ARRAY['5','6','7'])   

returned error: function array_cat(character varying[], text[]) does not exist

返回错误: function array_cat(character varying[], text[]) does not exist

Reason error is that array types not matches right?

原因错误是数组类型不匹配吗?

Question: how to convert this array ARRAY['5','6','7']as CHARACTER VARYING[]type ?

问题:如何将此数组转换ARRAY['5','6','7']CHARACTER VARYING[]类型?

回答by Craig Ringer

Cast to varchar[]:

投射到varchar[]

 > SELECT ARRAY['5','6','7']::varchar[], pg_typeof( ARRAY['5','6','7']::varchar[] );

 SELECT ARRAY['5','6','7']::varchar[], pg_typeof( ARRAY['5','6','7']::varchar[] );
  array  |      pg_typeof      
---------+---------------------
 {5,6,7} | character varying[]

You can use the PostgreSQL specific ::varchar[]or the standard CAST(colname AS varchar[])... though as arrays are not consistent across database implementations there won't be much advantage to using the standard syntax.

您可以使用特定于 PostgreSQL 的::varchar[]或标准的CAST(colname AS varchar[])......尽管由于数组在数据库实现中不一致,使用标准语法不会有太大优势。