是否可以根据另一列中的公共属性值列出一列的属性值

时间:2014-06-08 16:42:30

标签: mysql sql

select * from categories limit 40;
+--------------+---------------+-----------------+------------
| category_id  | T1            | T2              | T3         
+--------------+---------------+-----------------+------------
             7 | Soft Drinks   | Fountain        | Apple               
            10 | Soft Drinks   | Fountain        | Clear               
            10 | Soft Drinks   | Fountain        | Lemon               
            15 | Soft Drinks   | Tin             | Clear               
            16 | Soft Drinks   | Bottled         | Apple               
            16 | Soft Drinks   | Bottled         | Apple               
            17 | Soft Drinks   | Bottled         | Mango               

我有4列,如下所示(category_id,T1,T2,T3)

如何根据T1对数据进行分组? (即本例中的软饮料)

例如,如下所示的层次结构

SoftDrinks有三个不同的类别,每个类别都可以包含category_id的子类别

SoftDrinks ==> Fountain ==> Apple(7) , Clear(10) , Lemon(10) .
SoftDrinks ===> Tin  =====> Clear(15)
SoftDrinks ===> Bottled  =====> Apple(16) , Apple(16) , Mango(17)

解决此问题的最佳方法是什么?

提前致谢??

1 个答案:

答案 0 :(得分:1)

select t1, t2, group_concat(concat(t3,'(',cateogoryId,')'),', ') x

from something

group by 1,2