开发者

SQL select query to merge rows

开发者 https://www.devze.com 2022-12-30 17:40 出处:网络
I have a single MySQL database: ------------------------------------------------------------------- Email| Name| Tag

I have a single MySQL database:

-------------------------------------------------------------------
Email                 | Name                      | Tag
---------------开发者_StackOverflow----------------------------------------------------
test@test.com         |Test Person               | TagOne
test@test.com         |Test Person               | Tag Two
-------------------------------------------------------------------

Need an SQL query that will return

-------------------------------------------------------------------
Email                 | Name                      | Tag
-------------------------------------------------------------------
test@test.com         |Test Person               | TagOne, Tag Two
-------------------------------------------------------------------

How can I do this?


Use:

  SELECT t.email,
         t.name,
         GROUP_CONCAT(DISTINCT t.tag ORDER BY t.tag SEPARATOR ', ')
    FROM YOUR_TABLE t
GROUP BY t.email, t.name

Reference:

  • GROUP_CONCAT


For anyone else trying to do this and is stuck:

SELECT Email, Name, GROUP_CONCAT(Tag ORDER BY Tag ASC SEPARATOR ', ')
FROM Table
GROUP BY Email
0

精彩评论

暂无评论...
验证码 换一张
取 消