Postgresql 8.2.11中的Group_concat等效项 [英] Group_concat equivalent in postgresql 8.2.11
问题描述
我使用的是Postgres 8.2.11的旧版本。谁能告诉我与MySql的 group_concat
等效的Postgres 8.2.11。我试过 array_accum
, array_to_string
, string_agg
但它没有在此版本中不起作用
I am using a older version of Postgres 8.2.11. Can anyone tell me the equivalent of MySql's group_concat
for this Postgres 8.2.11. I have tried array_accum
, array_to_string
, string_agg
but it doesn't work in this version
推荐答案
注释中的不太重复 应该为您指明正确的方向:创建您自己的聚合函数。首先,您需要一个非聚合的字符串连接函数,如下所示:
The "not quite duplicate" in the comments should point you in the right direction: create your own aggregate function. First you'll need a non-aggregate string concatenation function, something like this:
create function concat(t1 text, t2 text) returns text as $$
begin
return t1 || t2;
end;
$$ language plpgsql;
然后,您可以定义该函数的聚合版本:
Then you can define your own aggregate version of that function:
create aggregate group_concat(
sfunc = concat,
basetype = text,
stype = text,
initcond = ''
);
现在,您可以 group_concat
全部:
select group_concat(s)
from t
group by g
我从档案库中挖了这个,但我认为它应该在8.2版中可以工作。
I dug this out of my archives but I think it should work in 8.2.
请记住,不再支持8.2,因此您可能希望尽快升级到至少8.4。
Keep in mind that 8.2 is no longer supported so you might want to upgrade to at least 8.4 as soon as possible.
这篇关于Postgresql 8.2.11中的Group_concat等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!