第一步。建立无限分类表。
create table if not exists `chi_category` ( `id` int(11) not null auto_increment, `dishcategory_path` varchar(255) default null, dishcategory_sort int(11), `dishcategory_name` varchar(255) default null, primary key (`id`)) engine=myisam default charset=utf8 auto_increment=11 ;
第二步。使用以下查询语句
select `id`,`dishcategory_path`,`dishcategory_name`,`dishcategory_sort`,concat(`dishcategory_path`,'-',id) as fullpath from `chi_category` order by fullpath
第三步。看上面的语句结尾时order by fullpath。这个时候大致排序是正确的。
但是dishcategory_sort这个字段的值没有排序。
因为order by要求当按照多个列进行排序时,只有第一列相同时才使用第二列。但是第一组fullpath是不可能相同的。所以现在不知道dishcategory_sort这个字段的值如何排序。
我想实现的正确的情况是先按fullpath排序,然后 子分类例如 (川菜、卤菜)应该在上级分类(吃的)之下进行子分类的排序,如图所示 卤菜应该排在川菜前面才对。
那么现在怎么处理呢?各位大神
回复内容: 第一步。建立无限分类表。
create table if not exists `chi_category` ( `id` int(11) not null auto_increment, `dishcategory_path` varchar(255) default null, dishcategory_sort int(11), `dishcategory_name` varchar(255) default null, primary key (`id`)) engine=myisam default charset=utf8 auto_increment=11 ;
第二步。使用以下查询语句
select `id`,`dishcategory_path`,`dishcategory_name`,`dishcategory_sort`,concat(`dishcategory_path`,'-',id) as fullpath from `chi_category` order by fullpath
第三步。看上面的语句结尾时order by fullpath。这个时候大致排序是正确的。
但是dishcategory_sort这个字段的值没有排序。
因为order by要求当按照多个列进行排序时,只有第一列相同时才使用第二列。但是第一组fullpath是不可能相同的。所以现在不知道dishcategory_sort这个字段的值如何排序。
我想实现的正确的情况是先按fullpath排序,然后 子分类例如 (川菜、卤菜)应该在上级分类(吃的)之下进行子分类的排序,如图所示 卤菜应该排在川菜前面才对。
那么现在怎么处理呢?各位大神
把dishcategory_sort 放入fullpath和dishcategory_path
mysql> insert into chi_category values(11, '0-0', 0), (12, '0-0', 0), (13, '0-0-0-11', 2), (14, '0-0-0-11', 1);query ok, 4 rows affected (0.00 sec)records: 4 duplicates: 0 warnings: 0mysql> select `id`,`dishcategory_path`,`dishcategory_sort`,concat(`dishcategory_path`,'-',dishcategory_sort, '-', id) as fullpath from chi_category order by fullpath;+----+-------------------+-------------------+---------------+| id | dishcategory_path | dishcategory_sort | fullpath |+----+-------------------+-------------------+---------------+| 11 | 0-0 | 0 | 0-0-0-11 || 14 | 0-0-0-11 | 1 | 0-0-0-11-1-14 || 13 | 0-0-0-11 | 2 | 0-0-0-11-2-13 || 12 | 0-0 | 0 | 0-0-0-12 |+----+-------------------+-------------------+---------------+4 rows in set (0.00 sec)