mysql 遞迴排序查詢 樹節點生成

2021-06-06 04:38:47 字數 959 閱讀 3346

mysql 遞迴排序查詢

備註:producttype  排序表,producttype。ptype父節點  ,producttype 。id 主鍵,showtreenodes (in rootid int) 函式,引數為起始型別rootid.

//我的部落格

drop procedure if exists  showtreenodes;

create procedure showtreenodes (in rootid int)

begin

declare level int ;

drop table if exists tmplst;

create table tmplst (

id int,

nlevel int,

scort varchar(8000)

);set level=0 ;

insert into tmplst select id,level,id from producttype where ptype=rootid;

while row_count()>0 do

set level=level+1 ;

insert into tmplst

select a.id,level,concat(b.scort,a.id) from producttype a,tmplst b

where  a.ptype=b.id and b.nlevel=level-1  ;

end while;

end;

//我的部落格

call showtreenodes(-1);

//我的部落格

select concat(space(b.nlevel*2),'┕',a.name)

from producttype a,tmplst b

where a.id=b.id

order by b.scort;

Mysql 遞迴查詢子節點

查詢父編碼及以下所有子節點 select id from select t1.id,t1.parent id,if find in set t1.id,pids 0,pids,if find in set t1.parent id,pids 0,pids concat ws pids,id 0 as...

MySQL遞迴查詢父子節點

create table folder id bigint 20 not null,parent id bigint 20 default null,primary key id 建立函式 create function getparlist rootid bigint returns varcha...

MySQL 遞迴查詢 當前節點及子節點

1.表結構 2.查詢語句 create procedure pro getunderorg inidd varchar 36 begin declare lev int setlev 1 drop table ifexists tmp1 create table tmp1 id varchar 36...