创建表SQL
DROP TABLE IF EXISTS `tab_category`;
CREATE TABLE `tab_category` (
`id` int(11) unsigned NOT NULL auto_increment,
`catename` varchar(30) default '' COMMENT '分配名称',
`sort` int(11) unsigned default '0' COMMENT '排序',
`path` varchar(50) default '0',
`pid` int(11) unsigned default '0' COMMENT '父级id',
PRIMARY KEY (`id`),
KEY `catename` (`catename`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
测试数据
INSERT INTO `tab_category` VALUES ('1', 'A', '0', '0', '0');
INSERT INTO `tab_category` VALUES ('2', 'B', '0', '0', '0');
INSERT INTO `tab_category` VALUES ('3', 'A-1', '0', '0-1', '1');
INSERT INTO `tab_category` VALUES ('4', 'A-2', '0', '0-1', '1');
INSERT INTO `tab_category` VALUES ('5', 'B-1', '0', '0-2', '2');
INSERT INTO `tab_category` VALUES ('6', 'B-2', '0', '0-2', '2');
INSERT INTO `tab_category` VALUES ('7', 'B-1-1', '0', '0-2-5', '5');
INSERT INTO `tab_category` VALUES ('8', 'B-1-2', '0', '0-2-5', '5');
组装SQL查询
SELECT id,catename,path,concat(path,'-',id) as fullpath
FROM tab_category ORDER BY fullpath asc;
无限极分类-全路径方式.png查询结果
网友评论
1 分类1 0 0-1
4 分类11 0-1 0-1-4
10 分类112 0-1-4 0-1-4-10
9 分类111 0-1-4 0-1-4-9
5 分类12 0-1 0-1-5
- --- -- --- ------------------------
10 分类112 0-1-4 0-1-4-10
9 分类111 0-1-4 0-1-4-9
当是两位数的时候10跟9比较 变成了9比1大