【问题标题】:GROUP_CONCAT() eqivalent in postgres [duplicate]postgres中的GROUP_CONCAT()等价物[重复]
【发布时间】:2020-02-14 00:15:44
【问题描述】:

在 MySQL 中,我使用 GROUP_CONCAT() 函数,但现在我需要将其转换为 Postgres。谁能帮助我如何将此类查询转换为 Postgres?我认为唯一需要替换的是GROUP_CONCAT?

SET @sql = NULL;
SELECT CONCAT(
   'SELECT ',GROUP_CONCAT(c.TABLE_NAME,'.',c.COLUMN_NAME,' AS `',c.TABLE_NAME,'.',c.COLUMN_NAME,'`'),'
    from t1 AS thre
inner join t2 as ter on thre.datasource = ter.datasource 
inner join t3 as ston on thre.datasource = ston.datasource
inner join t4 as diron on thre.datasource = diron.datasource'
)
INTO @sql
FROM INFORMATION_SCHEMA.COLUMNS c
WHERE c.TABLE_NAME IN ('t1','t2',
                       't3','t4');    
PREPARE sql_statement FROM @sql;
EXECUTE sql_statement;

【问题讨论】:

标签: sql postgresql string-aggregation


【解决方案1】:

您可以使用STRING_AGG

select 'SELECT '||string_agg(c.TABLE_NAME||'.'||c.COLUMN_NAME||' AS '
                           ||c.TABLE_NAME||'.'||c.COLUMN_NAME ,',') ||
            'from t1 AS thre inner join t2 ..
            ..
            .. = diron.datasource'              
 from INFORMATION_SCHEMA.COLUMNS c 

|| 是 Postgres 中的连接运算符

【讨论】:

  • 嗯,这不起作用,我在结果中得到一列的值
【解决方案2】:

我认为您正在寻找string_agg() 函数

https://www.postgresql.org/docs/12/functions-aggregate.html

【讨论】:

    猜你喜欢
    • 2015-02-06
    • 1970-01-01
    • 2010-11-10
    • 2011-09-12
    • 1970-01-01
    • 1970-01-01
    • 2014-02-15
    • 2012-05-19
    相关资源
    最近更新 更多