【问题标题】:Concat multiple rows PSQL连接多行 PSQL
【发布时间】:2017-09-24 19:30:12
【问题描述】:
     id  |     name      |   Subject   | Lectured_Times |     Faculty                      
 3258132 | Chris Smith   | SATS1364    |     10         | Science 
 3258132 | Chris Smith   | ECTS4605    |      9         | Engineering

我将如何创建以下内容

3258132  Chris Smith SATS1364, 10, Science + ECTS4605, 9,Engineering

其中 + 只是一个新行。注意'+'(新行)之后它没有连接id,name

【问题讨论】:

标签: sql postgresql string-concatenation


【解决方案1】:

试试

SELECT distinct concat(id,"name",string_agg(concat(subject, Lectured_Times , Faculty), chr(10))) 
from tn 
where id = 3258132 
group by id;

【讨论】:

    【解决方案2】:

    如上所述,string_agg 是完美的解决方案。

    select 
        id, name, string_agg(concat(subject, Lectured_Times, Faculty), '\n')
    from table
    group by id, name
    

    【讨论】:

      猜你喜欢
      • 2013-11-22
      • 2020-07-16
      • 1970-01-01
      • 2014-09-23
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 2017-04-19
      • 1970-01-01
      相关资源
      最近更新 更多