如何在mysql中分类

时间:2014-11-29 19:00:33

标签: mysql sql

想想我有一张如下所示的表

**id**                           **product**

2                                 chocolate
1                                 chocolate    
2                                 pepsi
3                                 fanta
2                                 pepsi
4                                 chocolate
5                                 chips
3                                 pizza
1                                 coke
2                                 chips
6                                 burger
7                                 sprite
0                                 pepsi

并希望仅使用mysql

以下面显示的方式排列上表
**id**                   **product**
0                          pepsi
1                          chocolate, coke
2                          chocolate,fanta,chips      
3                          fanta,pizza
4                          chocolate
5                          chips
6                          burger
7                          sprite

我该怎么做?

1 个答案:

答案 0 :(得分:1)

select id, group_concat(distinct product) as products
from your_table
group by id
order by id