Postgres总结了所有

时间:2013-01-21 01:04:32

标签: sql postgresql postgresql-9.1

我需要将每行的所有列(未知列)连接到一个字符串

我想要这样做:

select concat(*) from table;

问题是函数concat不带参数“*”

任何人都可以帮助我?

示例:

GID | Name
----------
1   | nameA
2   | nameB
3   | nameC

我需要输出:

1nameA
2nameB
3nameC

3 个答案:

答案 0 :(得分:8)

select rtrim(ltrim(replace(tablename::text, ',', ''), '('), ')') from tablename;

答案 1 :(得分:5)

我会提出两种选择。它们都使用table_row to text技术。

  1. 又快又脏:

    select r::text from some_table AS r
    

    示例输出:

    (289,310,,2010-09-10,6,0,1,6,0,30514,6,882,8,4,1,7,2,2,3,1,2,2,2,1,2,2,2,,1,51,0,0,0,0,0,1386,1,1,,6,,0,,,010100002082080000B3EA73156DA25C411E85EB61CB155641)
    
  2. 快速操作数据:

    select translate(string_to_array(r::text, ',')::text, '()', '')::text[] from some_table AS r
    

    返回一个实际的文本数组(text[]),可以在其上应用任何数组函数:) 样本输出(注意开始和结束括号类型):

    {289,310,"",2010-09-10,6,0,1,6,0,30514,6,882,8,4,1,7,2,2,3,1,2,2,2,1,2,2,2,"",1,51,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1386,1,1,"",6,"",0,"","",010100002082080000B3EA73156DA25C411E85EB61CB155641}
    
  3. 使用第二种方法和array_to_string(array, delimiter)函数,您可以将所有列文本表示连接成一个字符串。只需选择一个分隔符(例如',''|''')。例如,使用'|',您最终会得到一个查询:

    select array_to_string(translate(string_to_array(r::text, ',')::text, '()', '')::text[], '|') from some_table AS r
    

    带样本输出:

    289|310||2010-09-10|6|0|1|6|0|30514|6|882|8|4|1|7|2|2|3|1|2|2|2|1|2|2|2||1|51|0|0|0|0|0|0|0|1|0|0|0|0|0|0|0|2|0|0|0|0|0|0|0|1|0|0|0|0|0|0|0|1|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|1386|1|1||6||0|||010100002082080000B3EA73156DA25C411E85EB61CB155641
    

    希望有所帮助:)

答案 2 :(得分:0)

尝试select concat(gid, user) from foo;,如this fiddle