表是這樣設計的
2,4,5字段請無視
現在要查詢出來這樣格式的數據
一級分類
二級分類A
三級分類A
三級分類B
二級分類B
三級分類A
三級分類B
我使用的sql為 select * from vn_erp_healthprice order by sorts
sorts字段為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值來排序的