查询部门树sql,通过parentId 查询该部门下的所有子部门(postgresql),其他数据库还未尝试过可不可用
WITH RECURSIVE tree ( organization_id, organization_name, parent_id ) AS (
SELECT
organization_id,
organization_name,
parent_id
FROM
core_organization
WHERE
organization_id = #{organizationId} AND delete_status = 'N'
UNION ALL
SELECT
e2.organization_id,
e2.organization_name,
e2.parent_id
FROM
core_organization e2,
tree
WHERE
tree.organization_id = e2.parent_id AND delete_status = 'N'
)
SELECT * FROM tree