表:gc_jzst_single_base
需求:要求返回这张表里符合条件的数据,且有父子级关系的,展示为同一组且分级,给后续业务调用
代码
WITH RECURSIVE t1 AS (
SELECT
single_id,
old_build_single_id,
single_name,
bulid_code,
1 AS LEVEL,
single_id groupFlag
FROM
gc_jzst_single_base
WHERE
single_assort = '1'
AND build_status != '0'
AND single_type = 'DWGC_DTLX_0802'
AND old_build_single_id IS NULL UNION ALL
SELECT
b.single_id,
b.old_build_single_id,
b.single_name,
b.bulid_code,
t1.LEVEL + 1,
t1.groupFlag
FROM
gc_jzst_single_base b
JOIN t1
WHERE
b.old_build_single_id = t1.single_id
AND b.single_assort = '1'
AND b.build_status != '0'
AND b.single_type = 'DWGC_DTLX_0802'
AND b.old_build_single_id IS NOT NULL
) -- 终止条件部分
SELECT
*
FROM
t1
ORDER BY
groupFlag;
结果:满足条件