php+mysql新无线级分类

create table cat(
    id int unsigned not null primary key auto_increment,
    pid int unsigned not null default 0,
    path varchar(200) not null default '',
    name char(30) not null default ''
)engine=MyISAM default charset=utf8;

insert into cat (pid,path,name) values(0,0,'soft'),(0,0,'net'),(1,'0,1','java'),(1,'0,1','php'),(3,'0,1,3','j2se'),(3,'0,1,3','j2ee'),(4,'0,1,4','smarty'),(4,'0,1,4','thinkphp'),(8,'0,1,4,8','xmlyz'),(6,'0,1,3,6','jsp');

select id, concat(path, ',', id) as abspath from cat order by abspath,id;

注意:其实这样就不用php进行递归来排序,减少性能开出。但是这样数据库的设计就有点冗余,path就是一个没有用的字段。但是综合起来更合理,一条sql语句就能搞定了

原文地址:https://www.cnblogs.com/shiwenhu/p/4828096.html