SQL select查询以合并行

时间:2010-05-16 20:58:47

标签: sql mysql group-concat

我有一个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
-------------------------------------------------------------------

我该怎么做?

2 个答案:

答案 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