Postgres数组值必须开始

问题描述 投票:-1回答:2

我想制作一个数组并加入两个id,但我犯了一个错误:

数组值必须以“{”或维度信息开头

ids_list character varying[] := ' || (SELECT COALESCE(quote_literal((array_agg(DISTINCT house_guid)) || ''',''' || quote_literal(array_agg(DISTINCT guid))), 'NULL') FROM tb) || ';
sql arrays postgresql
2个回答
0
投票

使用array_agg功能

with t1 as 
(
select * from 
(
select 'test_SQL_01' as ID
union
select 'test_SQL_02_PQR_01'
union
select 'test_SQL_03_055'
union
select 'test_SQL_04_ABC_99'
) as t
) select array_agg(ID) from t1

0
投票

你似乎在PL / pgSQL函数中使用它。你应该使用SELECT ... INTO variable FROM...代替:

declare
  ids_list character varying[];
begin
  ..... 
  select array_agg(id)
    into ids_list
  from (
    select house_guid
    from tab
    union 
    select guid
    from tab
  ) t;

  .... work with the ids_list variable
end;

UNION将自动删除所有重复项(正如您尝试使用DISTINCT一样)。

© www.soinside.com 2019 - 2024. All rights reserved.