开发者

SQL select query to merge rows

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜