mysql儲存過程系列一 遞迴查詢

2021-09-30 10:47:11 字數 3893 閱讀 9096

專案中需要用到遞迴查詢的功能,所以研究了一下。

首先表的結構如下:

table: tb_department

field

type

comment

idbigint(20) not null

機構編號

seq_no

varchar(100) null

機構編碼(規則:省級機構:1-9,市級:10-99,縣級100-999,網格:大於1000)

parent_seq_no

bigint(20) null

上級機構編碼

name

varchar(50) null

機構名flag

int(1) null

機構所屬的級別(1表示省級,2表示市級,3表示縣級,4表示網格級別)

priority

int(4) null

機構排序

remarks

varchar(500) null備註

只有上面標註為紅色的字段會用到,其他的字段不需要用到。這張表存放的是部門的資訊,其中parent_seq_no存放的是上級部門的id號(注意不是上級部門的seq_no)。這樣就構成了上下級關係,就可以形成遞迴查詢的條件了。

建立表的sql為: 

create table `tb_department` (

`id` bigint(20) not null auto_increment comment '機構編號',

`seq_no` varchar(100) character set utf8 default null comment '機構編碼(規則:省級機構:1-9,市級:10-99,縣級100-999,網格:大於1000)',

`parent_seq_no` bigint(20) default null comment '上級機構編碼',

`name` varchar(50) character set utf8 default null comment '機構名',

`flag` int(1) default null comment '機構所屬的級別(1表示省級,2表示市級,3表示縣級,4表示網格級別)',

`priority` int(4) default null comment '機構排序',

`remarks` varchar(500) collate utf8_bin default null comment '備註',

primary key (`id`),

unique key `seq_no_unique` (`seq_no`),

unique key `name_unique` (`name`)

) engine=innodb auto_increment=15 default charset=utf8 collate=utf8_bin comment='部門表,在組織機構管理中的部門管理的資訊儲存在這張表中'

表中的資料暫時有如下幾行:

idseq_no

parent_seq_no

name

flag

priority

remarks11

(null)

湖北省1

1(null)22

1武漢市22

(null)

30001

2洪山區

31000

(null)

40002

2青山區

310001

(null)

510001

3洪山區網格1

410002

(null)

610003

3洪山區網格2

410004

(null)

710005

4青山區網格1

410006

(null)

810007

4青山區網格2

410008

(null)

100003

2江岸區321

而110030

10一元路網格

32223

(null)

120032

10解放大道網格323

(null)

130031

10沿江大道網格312

(null)

140033

10分銷渠道網格

9999

(null)

下面這張圖列出了部門的上下級關係:

現在要做的是,給定乙個部門的id,查詢出他下面所有的子部門。如給定部門id為3,那麼需要輸出3,5,6

需要編寫兩個儲存過程,乙個是基礎的儲存過程,執行遞迴操作,命名為:findchildlist,還有乙個是呼叫該儲存過程的儲存過程,使用遞迴儲存過程執行後的結果,命名為:finddeplist。

首先定義執行遞迴操作的儲存過程:findchildlist

create definer = 'root'@'%'

procedure findchildlist(in departmentid bigint)

begin

declare v_dep 

integer default -1;

declare done integer default 0;

declare c_dep cursor for select d.id

from

tb_department d

where

d.parent_seq_no = departmentid;

declare continue handler for not found set done=1;

set @@max_sp_recursion_depth = 10;

insert into tmp_dep values (departmentid);

open c_dep;

fetch c_dep into v_dep;

while (done =0)

do call findchildlist(v_dep);

fetch c_dep into v_dep;

end while;

end

再定義呼叫遞迴操作的儲存過程:finddeplist

create definer = 'root'@'localhost'

procedure finddeplist(in departmentid bigint)

begin

drop temporary table if exists tmp_dep;

create temporary table tmp_dep(

depid integer

); delete

from

tmp_dep;

call findchildlist(departmentid);

select distinct depid

from

tmp_dep order by depid;

end

現在在mysql中建立好這兩個儲存過程,並且執行 call finddeplist(3)就會輸出3,5,6

臨時表是執行遞迴查詢的關鍵,要想使用儲存過程執行後的結果,使用臨時表是一種很好的辦法,在這個例子中,在主調程式(finddeplist)中建立好了臨時表tmp_dep,在被呼叫程式(findchildlist)中往臨時表中插入資料,最後在主調程式中可以使用遞迴查詢後的資料。注意:單純執行findchildlist會報語法錯誤。這樣在我們的業務邏輯中如果需要查詢乙個部門的子部門,只需要將finddeplist的**移植過去,既建立中間表,然後執行儲存過程,最後就可以從中間表中查詢出需要的結果。

MySQL儲存過程遞迴呼叫

有分類表tb system category,結構如下 create table tb system category id int 11 not null auto increment,c parent id int 11 not null,c name varchar 50 not null,c...

mysql儲存過程使用遞迴

實現功能為查詢節點所有子節點同時更新子節點資料,用遞迴進行處理。測試時始終報錯 error code 1456 set max sp recursion depth 100 下面是mysql遞迴呼叫的源 delimiter in uid varchar 225 in ncount int begin...

mysql儲存過程(遞迴,交叉表)

1.遞迴 delimiter drop procedure if exists rentmgr tmp 引數說明 zjjg id上級機構編號,result是輸出變數 函式說明 遞迴返回本級機構和下級所有機構編號,通過 分隔 create definer root localhost procedur...