2013-03-22 51 views
1

我需要编写一个将select列中的值组合到一个单元格中的SQL select语句。在一个单元格中将列值组合在一起

例如

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.我知道数据不是以一种特别明智的方式布置的,但我无法改变这一点。

回答

1

你想group_concat()

select customerId, age, group_concat(hobby) as hobbies 
from t 
group by customerId, age 
+0

先生,你有各种真棒,我甚至不能开始描述。 – Urbycoz 2013-03-22 13:14:50

相关问题