2

我有一张这样的桌子:

 ID | word
 ___________

 1  | hello
 1  | goodbye
 1  | goodnight
 2  | What
 2  | Why
 3  | Yes
 3  | No

有没有办法收集具有相同 ID 的单词并将其显示在一行中,如下所示:

 ID | word
 _______________________________

 1  | hello, goodbye, goodnight
 2  | What, Why
 3  | Yes, No
4

3 回答 3

7

采用GROUP_CONCAT()

SELECT ID, GROUP_CONCAT(word SEPARATOR ', ') word
FROM tableName
GROUP BY ID

输出

╔════╦═══════════════════════════╗
║ ID ║           WORD            ║
╠════╬═══════════════════════════╣
║  1 ║ hello, goodbye, goodnight ║
║  2 ║ What, Why                 ║
║  3 ║ Yes, No                   ║
╚════╩═══════════════════════════╝
于 2013-05-21T07:43:02.863 回答
1

按“ID”分组并使用函数group_concat

于 2013-05-21T07:43:51.663 回答
1

GROUP_CONCAT

select ID, group_concat(word separator ', ') from yourTable group by ID;
于 2013-05-21T07:44:56.487 回答