为什么这个查询不起作用? (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;
错误:
您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解第 18 行 '' 附近使用的正确语法(第 18 行是 INNER JOIN)。
我遵循了另一篇文章中的确切语法(How to create a MySQL分层递归查询?),所以我不知道哪里出了问题。任何帮助将不胜感激!
I have the following 2 tables and attempting a recursive query. Below is the tables and the query which is giving me an error.
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;
The error:
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).
I've followed the exact syntax from another post (How to create a MySQL hierarchical recursive query?), so I cannot figure out where I am going wrong. Any help would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的链接项目在查询的三个部分中恰好显示三列。但是您在这里有三列...
和四列...
并在此处
删除额外的列修复您的语法错误小提琴。
您可能需要像这样给第四列自己的名称(
ancestor_tag_id
?)。 fiddle> fiddle这些再生ctes的语法错误报告是疯狂而无用的,eh? :-)
Your linked item shows exactly three columns in three parts of its query. But you have three columns here...
and four columns here ...
and here
Removing the extra column fixes your syntax error fiddle.
You may want to give that fourth column its own name (
ancestor_tag_id
maybe?) like this. fiddleThe syntax error reports for these recursive CTEs are maddening and useless, eh? :-)