问题描述
- mysql无限极分类排序问题
-
表是这样设计的
2,4,5字段请无视
现在要查询出来这样格式的数据
一级分类
二级分类A
三级分类A
三级分类B
二级分类B
三级分类A
三级分类B
我使用的sql为 select * from vn_erp_healthprice order by sortssorts字段为varchar类型
查询的结果如下
排序结果满足业务要求,但为什么1-11排在1-2前面
还有 根据字符串排序这是个什么原理来着 好久没用过这种分类了 一时想不起来了
求大神赐教表sql如下
/* Navicat MySQL Data Transfer Source Server : localhost Source Server Version : 50621 Source Host : 127.0.0.1:3306 Source Database : test Target Server Type : MYSQL Target Server Version : 50621 File Encoding : 65001 Date: 2015-07-15 16:38:49 */ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for `vn_erp_healthprice` -- ---------------------------- DROP TABLE IF EXISTS `vn_erp_healthprice`; CREATE TABLE `vn_erp_healthprice` ( `id` int(10) NOT NULL AUTO_INCREMENT COMMENT '主键', `lyid` varchar(10) NOT NULL DEFAULT '' COMMENT '维修项id', `name` varchar(100) NOT NULL DEFAULT '' COMMENT '维修项名称', `price` varchar(10) NOT NULL DEFAULT '' COMMENT '工时单价', `hours` varchar(10) NOT NULL DEFAULT '' COMMENT '工时', `parent_id` int(11) NOT NULL DEFAULT '0', `sorts` varchar(20) NOT NULL DEFAULT '' COMMENT '排序', PRIMARY KEY (`id`), KEY `hid` (`lyid`) ) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of vn_erp_healthprice -- ---------------------------- INSERT INTO `vn_erp_healthprice` VALUES ('1', '', '保养(一级分类)', '', '', '0', '1'); INSERT INTO `vn_erp_healthprice` VALUES ('2', '', '更换(二级分类)', '', '', '1', '1-2'); INSERT INTO `vn_erp_healthprice` VALUES ('3', '1', '更换机油', '', '', '2', '1-2-3'); INSERT INTO `vn_erp_healthprice` VALUES ('4', '2', '更换机油滤清器', '', '', '2', '1-2-4'); INSERT INTO `vn_erp_healthprice` VALUES ('5', '3', '更换空气滤清器', '', '', '2', '1-2-5'); INSERT INTO `vn_erp_healthprice` VALUES ('6', '4', '更换燃油滤清器', '', '', '2', '1-2-6'); INSERT INTO `vn_erp_healthprice` VALUES ('7', '5', '更换全部火花塞', '', '', '2', '1-2-7'); INSERT INTO `vn_erp_healthprice` VALUES ('8', '6', '检查助力转向油', '', '', '11', '1-2-8'); INSERT INTO `vn_erp_healthprice` VALUES ('9', '7', '更换整车制动液', '', '', '2', '1-2-9'); INSERT INTO `vn_erp_healthprice` VALUES ('10', '8', '检查自动变速箱油', '', '', '11', '1-11-10'); INSERT INTO `vn_erp_healthprice` VALUES ('11', '', '检查(二级分类)', '', '', '1', '1-11'); INSERT INTO `vn_erp_healthprice` VALUES ('12', '9', '检查或者更换手动变速箱油', '', '', '11', '1-11-12'); INSERT INTO `vn_erp_healthprice` VALUES ('13', '10', '检查发动机正时皮带', '', '', '11', '1-11-13'); INSERT INTO `vn_erp_healthprice` VALUES ('14', '11', '更换空调滤清器', '', '', '2', '1-2-14'); INSERT INTO `vn_erp_healthprice` VALUES ('15', '', '定期(二级分类)', '', '', '1', '1-15'); INSERT INTO `vn_erp_healthprice` VALUES ('16', '12', '定期添加冷媒', '', '', '15', '1-15-16'); INSERT INTO `vn_erp_healthprice` VALUES ('17', '13', '检查空调管路', '', '', '11', '1-11-17'); INSERT INTO `vn_erp_healthprice` VALUES ('18', '14', '更换防冻冷却液', '', '', '2', '1-2-18'); INSERT INTO `vn_erp_healthprice` VALUES ('19', '15', '检查厚度、调整,必要时更换前刹车片', '', '', '11', '1-11-19'); INSERT INTO `vn_erp_healthprice` VALUES ('20', '16', '检查厚度、调整,必要时更换前刹车盘', '', '', '11', '1-11-20'); INSERT INTO `vn_erp_healthprice` VALUES ('21', '17', '检查厚度、调整,必要时更换后刹车片(蹄)', '', '', '11', '1-11-21'); INSERT INTO `vn_erp_healthprice` VALUES ('22', '18', '检查厚度、调整,必要时更换后刹车盘(鼓)', '', '', '11', '1-11-21'); INSERT INTO `vn_erp_healthprice` VALUES ('23', '', '调整(二级分类)', '', '', '1', '1-22'); INSERT INTO `vn_erp_healthprice` VALUES ('24', '19', '调整雨刮(雨刷)', '', '', '23', '1-22-23'); INSERT INTO `vn_erp_healthprice` VALUES ('25', '20', '检查电瓶', '', '', '11', '1-11-24'); INSERT INTO `vn_erp_healthprice` VALUES ('26', '21', '检查减震器', '', '', '11', '1-11-25');
解决方案
字符串排序是ASCII吗排的,1-11与1-2前面的1-是相同的,就到1与2这2个对比,1与2的ASCII值来排序的
解决方案二:
改成先按长度排序,再按字母排序
select * from vn_erp_healthprice order by length(sorts),sorts
解决方案三:
字符串1-11
和1-2
,前两个字符串相等,第3个字符串'1'<'2'
,所以'1-11' < '1-2'
。
要按数值比较你得吧每一级都格式成等长,比如'001-011' > '001-002'
。
时间: 2025-01-02 00:54:32