如何按顺序使用group_concat

时间:2017-12-22 08:52:56

标签: mysql mysqli

我的表结构是

cat_id  cat_desc    parent_category par_cat_order   cdate               status
1       Machine                                     2017-12-22 13:10:51 active
2       Woods                                       2017-12-22 13:11:01 active
3       Tools                                       2017-12-22 13:11:10 active
4       Perol           1           1               2017-12-22 13:11:30 active
5       Diesel          1           1               2017-12-22 13:11:43 active
6       Table           2           2               2017-12-22 13:11:59 active
7       Chair           2           2               2017-12-22 13:12:13 active
8       Round Table     6           2,6             2017-12-22 13:12:32 active
9       Dining          6           2,6             2017-12-22 13:13:01 active
10      Cuting Player   3           3               2017-12-22 13:13:15 active
11      Scrow Driver    3           3               2017-12-22 13:13:40 active
12      Sandal          9           2,6,9           2017-12-22 13:32:23 active

我希望在两者中显示类别编号的父类别描述(' parent_category',' par_cat_order')

我的查询是

select a.cat_id,
a.cat_desc,
 b.cat_desc,
 group_concat(c.cat_desc) 
 from category a 
 left JOIN category b on (a.parent_category=b.cat_id) 
 left JOIN category c on find_in_set(c.cat_id,a.par_cat_order) 
 GROUP by a.cat_id

,输出

cat_id  cat_desc        cat_desc    group_concat(c.cat_desc)
1       Machine     
2       Woods       
3       Tools       
4       Perol           Machine     Machine
5       Diesel          Machine     Machine
6       Table           Woods       Woods
7       Chair           Woods       Woods
8       Round Table     Table       Table,Woods
9       Dining          Table       Table,Woods
10      Cuting Player   Tools       Tools
11      Scrow Driver    Tools       Tools
12      Sandal          Dining      Dining,Table,Woods

我需要的是' group_concat(c.cat_desc)'以存储的顺序显示字段。对于例如 我存储在' par_cat_order'作为第8记录的2,6。它将显示Woods,Table。但它显示了" Table,Woods"

1 个答案:

答案 0 :(得分:1)

您可以在$item[]

中使用order by
group_concat

如果您可以规范化表格结构Database normalization

,那会更好