Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何按顺序分组_Mysql - Fatal编程技术网

Mysql 如何按顺序分组

Mysql 如何按顺序分组,mysql,Mysql,我的桌子结构是 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

我的桌子结构是

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
我希望在这两种情况下都显示父类别描述而不是类别编号(“父类别”和“零件分类顺序”)

我的问题是

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”的形式存储为2,6,作为第8条记录。它将展示木头,桌子。但它会显示“Table,Woods”

您可以在
group\u concat中使用
order by

 select a.cat_id,
 a.cat_desc,
 b.cat_desc,
 group_concat(c.cat_desc order by c.cat_id asc) 
 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
如果可以规范化表结构,那就更好了