在 H2 上使用 CTE 查询找不到列

Column not found with CTE query on H2

我正在尝试了解 cte 的工作原理,但无法解决未找到列的错误。

这是我的 table:

CREATE TABLE IF NOT EXISTS NODE (UUID VARCHAR NOT NULL, PARENT_UUID VARCHAR NULL,
NAME VARCHAR NOT NULL, PRIMARY KEY (UUID));

这是我的查询:

WITH tree AS
(
    -- Anchor member
    SELECT * FROM node WHERE uuid = ''
    UNION ALL
    -- Recursive member that references expression_name.
    SELECT a.uuid, a.parent_uuid, a.name FROM node a
    INNER JOIN tree b ON b.parent_uuid = a.uuid
)
-- references expression name
SELECT * FROM tree;

这就是我得到的:Column "B.PARENT_UUID" not found。谁能帮帮我?

您的递归 CTE 可能略有偏差。考虑这个版本:

WITH RECURSIVE tree (uuid, parent_uuid, name) AS (
    SELECT uuid, parent_uuid, name FROM node WHERE uuid = ''
    UNION ALL
    SELECT a.uuid, a.parent_uuid, a.name
    FROM node a
    INNER JOIN tree b ON b.parent_uuid = a.uuid
)

SELECT * FROM tree;