--drop function `getchildlist`
create function `getchildlist`(rootid varchar(100))
returns longtext
begin
declare str longtext;
declare cid longtext;
set str = '$';
set cid = rootid;
while cid is not null do
set str = concat(str, ',', cid);
select group_concat(id) into cid from treenodes where find_in_set(parentid, cid) > 0;
end while;
return str;
end
select getchildlist('447');
select * from tbl_institution where find_in_set(id,getchildlist(200));
--drop function `getparentlist`
create function `getparentlist`(rootid varchar(100))
returns varchar(1000)
begin
declare fid varchar(100) default '';
declare str varchar(1000) default rootid;
while rootid is not null do
set fid =(select parentid from treenodes where id = rootid);
if fid is not null then
set str = concat(str, ',', fid);
set rootid = fid;
else
set rootid = fid;
end if;
end while;
return str;
end
select getparentlist('1088');
select * from tbl_institution where find_in_set(id,getparentlist(200));
問題說明
關於longtext的說明
在我實際使用過程中,varchar已經不滿足我所處理的資料長度,所以在此使用了longtext,最大長度為4 294 967 295個字元
關於使用group_concat字串過長被擷取的問題
修改配置檔案:my.ini
在[mysqld]下新增配置:group_concat_max_len = 102400
參考文章
關於父集合查詢說明
mysql省市區遞迴查詢 mysql 遞迴查詢
insert into t areainfo values 1 0 中國 0 0 insert into t areainfo values 2 0 華北區 1 0 insert into t areainfo values 3 0 華南區 1 0 insert into t areainfo va...
mysql遞迴查詢統計 mysql遞迴查詢
樣例資料 create table treenodes id int primary key,nodename varchar 20 pid int select from treenodes id nodename pid 1 a 0 2 b 1 3 c 1 4 d 2 5 e 2 6 f 3 7...
mysql 遞迴查詢效率 mysql 遞迴查詢
set stemp set stempchd cast id as char 轉化資料格式 while stempchd is not null do 開始迴圈 set stemp concat stemp,stempchd 字串拼接 select group concat id into stem...