0

我使用的是旧版本的 Postgres 8.2.11。谁能告诉我group_concat这个 Postgres 8.2.11 的 MySql 的等价物。试过了array_accum,这个版本不行array_to_stringstring_agg

4

1 回答 1

1

评论中的“不完全重复”应该为您指明正确的方向:创建自己的聚合函数。首先,您需要一个非聚合字符串连接函数,如下所示:

create function concat(t1 text, t2 text) returns text as $$
begin
    return t1 || t2;
end;
$$ language plpgsql;

然后您可以定义您自己的该函数的聚合版本:

create aggregate group_concat(
    sfunc    = concat,
    basetype = text,
    stype    = text,
    initcond = ''
);

现在你可以group_concat随心所欲:

select group_concat(s)
from t
group by g

我从我的档案中挖出了这个,但我认为它应该在 8.2 中工作。

请记住,不再支持 8.2,因此您可能希望尽快升级到至少 8.4。

于 2013-05-09T06:49:53.913 回答