SQL concatenate rows query

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

Say we have a table

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


        
相关标签:
3条回答
  • You didn't indicate a particular SQL engine.

    In Firebird (from 2.1) you can use the LIST() function. Take a look at: link text

    It's an aggregate function to do exactly what you need.

    I guess it exists in other engines (LIST in Sybase SQL Anywhere, GROUP_CONCAT in MySQL)

    0 讨论(0)
  • 2021-01-15 13:18

    If you use MySQL then you can use GROUP_CONCAT:

    SELECT status, GROUP_CONCAT(title)
    FROM posts
    GROUP BY status
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题