首先看一下表结构:
CREATE TABLE ide_iedp_project
(
project_id integer NOT NULL ,
project_code character varying(50),
project_name character varying(255),
access_token character varying(255),
parent_project_id integer,
CONSTRAINT ide_iedp_project_pkey PRIMARY KEY (project_id)
)
然后看一下表中数据:
再说一下取数逻辑:
根据传入的project_id,获取等于该project_id 及parent_project_id等于该值,以及递归下去的值。
最后,上代码:
WITH RECURSIVE project AS (
SELECT * FROM ide_iedp_project WHERE project_id = #{projectId}
union all
SELECT ide_iedp_project.* FROM ide_iedp_project, project WHERE ide_iedp_project.parent_project_id = project.project_id
)
SELECT project_id, project_name FROM project ORDER BY project_id