我的表格如下:
id title parent_id editor
441 A 0 2
1093 B 18 2
18 C 0 0
3145 D 0 0
3144 E 0 8
3140 F 3130 22
3141 G 3130 335
3130 H 0 0
从表中可以看出,id 为 3130 的标题 H 有两个 child ,分别为 id 为 3140 的标题 F 和 id 为 3141 的标题 G。 所以,如果我想调用 b.editor = 22,我的预期输出必须是
a.id a.title a.parent_id a.editor b.id b.title b.parent_id b.editor
3130 H 0 0 3140 F 3130 22
我该如何实现它?
最佳答案
SELECT a.id, a.title, a.parent_id, a.editor, b.id, b.title, b.parent_id, b.editor
FROM test AS a
INNER JOIN test AS b
ON a.id = b.parent_id
WHERE b.editor = 22
关于MySQL 从子级调用父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50832514/