SQL concatenate rows query

前端 未结 3 558
礼貌的吻别
礼貌的吻别 2021-01-15 12:53

Say we have a table

table posts
+---------+-----------+--------------------------------+
| postId  | title     | status    | bodyText           |
+--------         


        
3条回答
  •  孤城傲影
    2021-01-15 13:18

    In MySQL:

    SELECT  status, GROUP_CONCAT(title SEPARATOR ', ')
    FROM    posts
    GROUP BY
            status
    

    In PostgreSQL:

    SELECT  status,
            ARRAY_TO_STRING(
            ARRAY(
            SELECT  title
            FROM    posts pi
            WHERE   pi.status = po.status
            ))
    FROM    posts po
    GROUP BY
            status
    

提交回复
热议问题