这个不是什么新东西了,网上有很多方法,我也尝试了其中好的方法,都不尽人意,这是我最后我推荐的方法,共享一下,大家一起讨论,当然难如高手法眼,呵呵。我就直接贴代码了,有问题就联系我。
mysql中要有树形结构我认为表中应该有如写字段:
举例菜单表menu:
create table menu
(
id int not null auto_increment,
pid int,
name varchar(100) not null,
nlevel int,
scort varchar(8000),
primary key (id)
)
type = InnoDB;
alter table menu add constraint FK_Reference_67 foreign key (pid)
references menu (id) on delete restrict on update restrict
创建存储过程genNode_menu:
BEGIN
DECLARE Level int ;
Set Level=0 ;
update menu a inner join (SELECT id,Level,concat(',',ID,',') scort FROM menu WHERE pid is null) b on a.id=b.id
set a.nlevel=b.l