1

我需要编写一个 SQL 选择语句,将一列中的值组合到一个单元格中。

例如

table name: Customer_Hobbies
+------------+------------+-----------+
| CustomerId |     Age    |  Hobby    |
+------------+------------+-----------+
| 123        |     17     |  Golf     |
| 123        |     17     |  Football |
| 324        |     14     |  Rugby    |
| 627        |     28     |  Football |
+------------+------------+-----------+

应该回来...

+------------+------------+----------------+
| CustomerId |     Age    |  Hobbies       |
+------------+------------+----------------+
| 123        |     17     |  Golf,Football |
| 324        |     14     |  Rugby         |
| 627        |     28     |  Football      |
+------------+------------+----------------+

这可能吗?

注意我知道数据的排列方式不是特别合理,但我无法改变这一点。

4

1 回答 1

1

你想要group_concat()

select customerId, age, group_concat(hobby) as hobbies
from t
group by customerId, age
于 2013-03-22T13:12:38.260 回答