递归CTE中的CTE多次访问递归部分

时间:2019-11-29 22:17:32

标签: mysql recursion common-table-expression recursive-query

我有以下查询可在postgres下正常运行,希望也能在MySQL上运行:

WITH RECURSIVE dependent(id) AS (
    SELECT id FROM data_category WHERE id = :cat
UNION (
    WITH parents AS ( SELECT * FROM dependent)
        SELECT child_id AS id
        FROM data_category_1 dc 
        INNER JOIN parents ON dc.id = parents.id
      UNION
        SELECT child_id AS id
        FROM data_category_2 dc 
        INNER JOIN parents ON dc.id = parents.id
    )
)
SELECT id from dependent

我收到一条非常有用的错误消息: Error : In recursive query block of Recursive Common Table Expression 'dependent', the recursive table must be referenced only once, and not in any subquery

问题似乎出在递归部分内部。我需要这样做是因为我必须多次访问递归部分parents。在postgres下可以正常工作。

0 个答案:

没有答案