序文
現在の部門の下にあるすべての組織を取得します
達成
テーブル構造(簡易版)
表名:sys_organization
字段:oid、oname、parent_id
Mysql8.0 (現在の組織の下にあるすべての組織をクエリ)
ここでの使用法は SqlServer と同じですが、追加の RECURSIVE があります。
WITH RECURSIVE os AS (
SELECT oid,oname,parent_id FROM sys_organization WHERE oid = 3
UNION ALL
SELECT oo.oid,oo.oname,oo.parent_id FROM sys_organization oo INNER JOIN os oo1 ON oo.parent_id=oo1.oid
)
SELECT
o.oid,
o.oname
FROM
sys_organization o
INNER JOIN os ON os.oid = o.oid
Mysql5.7 (現在の組織の下にあるすべての組織をクエリ)
select oid,oname from (
select
t1.oid,
t1.oname,
if(find_in_set(parent_id, @pids) > 0, @pids := concat(@pids, ',', oid), 0) as ischild
from (
select
oid,
parent_id,
oname
from sys_organization t order by parent_id, oid
) t1,
(select @pids := 3) t2
) t3 where ischild != 0 OR oid = 3
order by oid
MySql5.7 は他のブログを参照しています: https://blog.csdn.net/Winston_Limf/article/details/97761515
説明:
- oid = 3 および @pids := 3 は現在のユーザーの部門 ID です。
- mysql のバージョン 5.7 では、oid = 3 を削除するか、@pids := 3 で値を変更すると、異なる効果が生じます。
効果
クエリ前
クエリ後