我有一个MySQL数据库:
-------------------------------------------------------------------
Email | Name | Tag
-------------------------------------------------------------------
test@test.com |Test Person | TagOne
test@test.com |Test Person | Tag Two
-------------------------------------------------------------------
需要返回的SQL查询
-------------------------------------------------------------------
Email | Name | Tag
-------------------------------------------------------------------
test@test.com |Test Person | TagOne, Tag Two
-------------------------------------------------------------------
我该怎么做?
答案 0 :(得分:3)
使用:
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
参考:
答案 1 :(得分:0)
对于其他任何试图这样做并被卡住的人:
SELECT Email, Name, GROUP_CONCAT(Tag ORDER BY Tag ASC SEPARATOR ', ')
FROM Table
GROUP BY Email