我需要编写一个SQL select语句,将一列中的值组合成一个单元格。
e.g。
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 |
+------------+------------+----------------+
这可能吗?
N.B。我知道这些数据并没有以一种特别合理的方式布局,但我无法改变它。
答案 0 :(得分:1)
您想要group_concat()
:
select customerId, age, group_concat(hobby) as hobbies
from t
group by customerId, age