为什么这个查询不起作用? (MySQL 8.0.28)

Why is this query not working? (MySQL 8.0.28)

我有以下 2 个表并尝试递归查询。下面是给我一个错误的表格和查询。

CREATE TABLE tags (
    tag_id INTEGER,
    tag_name VARCHAR(50) NOT NULL,
    tag_descrip VARCHAR(200),
    tag VARCHAR(200) NOT NULL,
    CONSTRAINT tags_pk PRIMARY KEY (tag_id )
);

CREATE TABLE tag_tree (
    tag_id INTEGER,
    parent_tag_id INTEGER,
    CONSTRAINT tag_tree_pk UNIQUE (tag_id, parent_tag_id),
    CONSTRAINT tag_tree_tags_fk FOREIGN KEY (tag_id)
    REFERENCES tags(tag_id),
    CONSTRAINT parent_tag_tags FOREIGN KEY (parent_tag_id)
    REFERENCES tags(tag_id),
    CONSTRAINT parent_tag_tag_tree_fk FOREIGN KEY (parent_tag_id)
    REFERENCES tag_tree(tag_id)
);

WITH RECURSIVE tags_and_their_parents (
    tag_id,
    parent_tag_id,
    depth)
AS (
    SELECT tag_id,
           parent_tag_id,
           parent_tag_id,
           0 AS depth
    FROM tag_tree
    WHERE parent_tag_id IS NULL

    UNION ALL

    SELECT tag_tree.tag_id,
           tag_tree.parent_tag_id,
           tags_and_their_parents.tag_id,
           tags_and_their_parents.depth + 1
    FROM tag_tree
    INNER JOIN tags_and_their_parents
    ON tag_tree.parent_tag_id = tags_and_their_parents.tag_id
)

SELECT * FROM tags_and_their_parents
ORDER BY depth;

错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 18 (Line 18 is the INNER JOIN).

我遵循了另一个 post (How to create a MySQL hierarchical recursive query?) 的确切语法,所以我不知道哪里出错了。任何帮助将不胜感激!

您的链接项在其查询的三个部分中恰好显示了三列。但是你这里有三列...

WITH RECURSIVE tags_and_their_parents (
    tag_id,
    parent_tag_id,
    depth)
AS ( ...

这里有四列...

   SELECT tag_id,
           parent_tag_id,
           parent_tag_id,
           0 AS depth

这里

    SELECT tag_tree.tag_id,
           tag_tree.parent_tag_id,
           tags_and_their_parents.tag_id,
           tags_and_their_parents.depth + 1

删除多余的列可以修复语法错误 fiddle

WITH RECURSIVE tags_and_their_parents (
    tag_id,
    parent_tag_id,
    depth)
AS (
    SELECT tag_id,
           parent_tag_id,
   --        parent_tag_id,
           0 AS depth
    FROM tag_tree
    WHERE parent_tag_id IS NULL

    UNION ALL

    SELECT tag_tree.tag_id,
           tag_tree.parent_tag_id,
    ---       tags_and_their_parents.tag_id,
           tags_and_their_parents.depth + 1
    FROM tag_tree
    INNER JOIN tags_and_their_parents
    ON tag_tree.parent_tag_id = tags_and_their_parents.tag_id

您可能希望为第四列指定自己的名称(ancestor_tag_id 也许?),就像这样。 fiddle

WITH RECURSIVE tags_and_their_parents (
    tag_id,
    parent_tag_id,
    ancestor_tag_id,  -- ADDED
    depth)
AS (
    SELECT tag_id,
           parent_tag_id,
           parent_tag_id ancestor_tag_id,  --CHANGED
           0 AS depth
    FROM tag_tree
    WHERE parent_tag_id IS NULL

    UNION ALL

    SELECT tag_tree.tag_id,
           tag_tree.parent_tag_id,
           tags_and_their_parents.tag_id ancestor_tag_id,  --CHANGED
           tags_and_their_parents.depth + 1
    FROM tag_tree
    INNER JOIN tags_and_their_parents

这些递归 CTE 的语法错误报告令人抓狂且毫无用处,是吗? :-)