一张表: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;