欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

查询-mysql无限极分类排序问题

程序员文章站 2022-06-05 16:34:35
...
mysql查询无限极分类排序

表是这样设计的
查询-mysql无限极分类排序问题
2,4,5字段请无视
现在要查询出来这样格式的数据
一级分类
二级分类A
三级分类A
三级分类B
二级分类B
三级分类A
三级分类B
我使用的sql为 select * from vn_erp_healthprice order by sorts

sorts字段为varchar类型
查询的结果如下
查询-mysql无限极分类排序问题
排序结果满足业务要求,但为什么1-11排在1-2前面
还有 根据字符串排序这是个什么原理来着 好久没用过这种分类了 一时想不起来了
求大神赐教

表sql如下

 /*Navicat MySQL Data TransferSource Server         : localhostSource Server Version : 50621Source Host           : 127.0.0.1:3306Source Database       : testTarget Server Type    : MYSQLTarget Server Version : 50621File Encoding         : 65001Date: 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');