Use the Data database table address data (China region) For example (using Windows please GBK!!) )
Can be run directly (remove annotations)
Stored procedures:
DELIMITER//
drop procedure if exists findlchild//
/* IID Recursive parent node, layer allow recursive depth * *
CREATE PROCEDURE Findlchild (IID bigint (), layer bigint (20))
BEGIN
/* Create a temporary form to accept the query * *
Create temporary table if not exists tmp_table (id bigint (m), name varchar ()) Engine=innodb DEFAULT Charset=utf8;
///maximum allowable recursive number * *
SET @ @max_sp_recursion_depth = 99;
Call iterative (Iid,layer);//Core Data collection * *
SELECT * FROM tmp_table/* Show * *
Drop temporary table if exists tmp_table;/* Delete temp form * *
end;//
DELIMITER;
DELIMITER//
drop procedure if exists iterative//
CREATE PROCEDURE Iterative (iid bigint (), layer bigint (20))
BEGIN
Declare tid bigint () default-1;
DECLARE tname varchar (m) Character set UTF8;
/* CURSOR Definition * *
Declare cur1 CURSOR for select Id,name from location where fid=iid;
Declare CONTINUE HANDLER for SQLSTATE ' 02000 ' SET tid = null;
/* Allow recursive depth * *
If Layer>0 Then
OPEN Cur1;
FETCH cur1 into Tid,tname;
While (Tid are not null)
Todo
/* Core Data collection * *
INSERT into tmp_table values (tid,tname);
Call iterative (tid,layer-1);
FETCH cur1 into Tid,tname;
End while;
End If;
end;//
DELIMITER;
Run!!
mysql> call findLChild(1,1);
+------+------------------+
| id | name |
+------+------------------+
| 2 | 北京 |
| 4 | 上海 |
| 6 | 香港特别行政区 |
| 8 | 澳门特别行政区 |
| 10 | 河北 |
| 23 | 山西 |
| 35 | 辽宁 |
| 50 | 吉林 |
| 60 | 黑龙江 |
| 74 | 江苏 |
| 88 | 浙江 |
| 101 | 安徽 |
| 119 | 福建 |
| 129 | 江西 |
| 142 | 山东 |
| 160 | 河南 |
| 179 | 湖北 |
| 198 | 湖南 |
| 213 | 广东 |
| 235 | 甘肃 |
| 250 | 四川 |
| 272 | 贵州 |
| 282 | 海南 |
| 301 | 云南 |
| 318 | 青海 |
| 327 | 陕西 |
| 348 | 广西壮族自治区 |
| 363 | 西藏自治区 |
| 371 | 宁夏回族自治区 |
| 377 | 新疆维吾尔自治区 |
| 400 | 内蒙古自治区 |
| 413 | 台湾省 |
+------+------------------+
32 rows in set (0.02 sec)