查詢 子部門sql

2021-09-29 09:35:19 字數 1048 閱讀 9660

1 建立儲存過程

-- ----------------------------

-- procedure structure for `getdeptchild`

-- ----------------------------

drop procedure if exists `getdeptchild`;

delimiter ;;

create definer=`sunxing_ccs`@`%` procedure `getdeptchild`(rootid varchar(50))

begin

declare ptemp varchar (4000);                     #用於儲存輸出的depart_id字串集

declare ctemp varchar (4000);                     #用於儲存當前的depart_id    

set ptemp = '';

set ctemp = rootid;

while ctemp is not null do             

if ptemp = '' then                 #判斷是否為第一次查詢,解決查詢結果第一位為','的問題

set ptemp = concat(ptemp, ctemp);

else

set ptemp = concat(ptemp, ',', ctemp);

end if;

#group_conca儲存後面符合條件的depart_id   find_in_set查詢當前ctemp裡是否有與depart_upid相等的值

select group_concat(dept_uuid) into ctemp from ccs_dept where find_in_set(parent_uuid, ctemp) > 0;

end while;

select ptemp;

end;;

delimiter ;

2 呼叫 

call getdeptchild(id)

查詢部門及其子部門相關方案總結

公司需求 匯入員工資訊時。需判斷當前管理員的許可權,當前管理員為超級管理員時可以匯入公司所有部門的員工資訊,部門管理員只能匯入本部門及以下子部門的員工資訊,特定部門只能匯入特定部門的資訊。思考 特定部門和所有部門的容易實現,但是查詢部門及其子部門不易實現。1.方案一 可以在service層進行遞迴迴...

使用標量函式建立無限的查詢子部門

使用標量函式建立無限的查詢子部門 不包含自身 alter function f getchildren pid int returns tree table tdtid int as begin insert tree select tdtid from tdept where tdtpid pid...

查詢所有上級部門的SQL

create table testdept deptid int identity 1,1 primary key,deptname varchar 16 superdept int insert into testdept deptname,superdept values 總經辦 0 inser...