1. 資料表見文章最後sql
2. 遞迴包含兩個要素:迴圈體,終止條件.net
3. 向上遞迴code
sql:blog
select遞迴
@co as 'code'開發
,( select @co := c.f_code from tree_test c where c.t_code = @co ) as 'f_code'字串
,@lv := @lv - 1 as 'level'get
fromclass
tree_test a ,( select @lv := b.t_level, @co := b.t_code from tree_test b where b.t_code = '300101') as xtest
where @lv > 0
分析:迴圈體: select @co := c.f_code from tree_test c where c.t_code = @co
以每次查詢的父級**做為下次查詢得分類**條件,一層層往上查詢
終止條件:層級,@lv 當查詢到頂級的實時結束查詢
4. 向下遞迴
sql:
select
@co as 'code'
,( select @co := group_concat(c.t_code)from tree_test c where find_in_set(c.f_code,@co)) as 'f_code'
from
tree_test a ,( select @co := group_concat(b.t_code) from tree_test b where b.t_code = '10') as x
where @co is not null
分析:迴圈體: select @co := group_concat(c.t_code)from tree_test c where find_in_set(c.f_code,@co) 以每次查詢的下級**集合做為下次查詢得分類**條件,一層層往下查詢,這裡用到了 分組合併字串group_concat()以及字串級查詢find_in_set()
終止條件:當沒有找到下級的資訊時,便可認為查詢完畢
5. myql 定義變數賦值 @param := 10
(以上僅作技術研究,實際開發中避免複雜sql)
樹級表:
create table `tree_test` (
`t_id` int(11) not null auto_increment comment '自增id',
`t_code` int(11) not null comment '節點**',
`t_name` varchar(200) default null comment '節點名稱',
`f_code` int(11) not null default '0' comment '父級節點',
`t_level` int(1) not null default '1' comment '節點層級',
primary key (`t_id`)
) engine=innodb auto_increment=15 default charset=utf8;
資料:insert into `tree_test` (`t_id`, `t_code`, `t_name`, `f_code`, `t_level`) values ('2', '10', '一級節點_1', '0', '1');
insert into `tree_test` (`t_id`, `t_code`, `t_name`, `f_code`, `t_level`) values ('3', '20', '一級節點_2', '0', '1');
insert into `tree_test` (`t_id`, `t_code`, `t_name`, `f_code`, `t_level`) values ('4', '30', '一級節點_3', '0', '1');
insert into `tree_test` (`t_id`, `t_code`, `t_name`, `f_code`, `t_level`) values ('5', '1001', '二級節點_1_1', '10', '2');
insert into `tree_test` (`t_id`, `t_code`, `t_name`, `f_code`, `t_level`) values ('6', '1002', '二級節點_1_2', '10', '2');
insert into `tree_test` (`t_id`, `t_code`, `t_name`, `f_code`, `t_level`) values ('7', '1003', '二級節點_1_3', '10', '2');
insert into `tree_test` (`t_id`, `t_code`, `t_name`, `f_code`, `t_level`) values ('8', '2001', '二級節點_2_1', '20', '2');
insert into `tree_test` (`t_id`, `t_code`, `t_name`, `f_code`, `t_level`) values ('9', '2002', '二級節點_2_2', '20', '2');
insert into `tree_test` (`t_id`, `t_code`, `t_name`, `f_code`, `t_level`) values ('10', '3001', '二級節點_3_1', '30', '2');
insert into `tree_test` (`t_id`, `t_code`, `t_name`, `f_code`, `t_level`) values ('11', '3002', '二級節點_3_2', '30', '2');
insert into `tree_test` (`t_id`, `t_code`, `t_name`, `f_code`, `t_level`) values ('12', '100201', '**節點_1_2_1', '1002', '3');
insert into `tree_test` (`t_id`, `t_code`, `t_name`, `f_code`, `t_level`) values ('13', '100202', '**節點_1_2_2', '1002', '3');
insert into `tree_test` (`t_id`, `t_code`, `t_name`, `f_code`, `t_level`) values ('14', '300101', '**節點_3_1_2', '3001', '3');
mysql 遞迴 mysql遞迴查詢
find in set 函式 函式語法 find in set str,strlist str 代表要查詢的字串 strlist 是乙個以逗號分隔的字串,如 a,b,c 此函式用於查詢 str 字串在字串 strlist 中的位置,返回結果為 1 n 若沒有找到,則返回0。concat 它用於連線n...
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...