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
精彩评论