MySQL - 在另一个UNION查询中使用查询结果

时间:2017-06-09 10:03:40

标签: mysql

如何将我从一个查询中获得的结果用于我加入UNION的另一个查询?我试图通过一次使用SQL查询来删除根链接及其所有子进程。我当然可以得到结果,然后用PHP进行另一个查询调用,但是如果可能的话,不想这样做。到目前为止,我已经找到了不起作用的代码:

SELECT id AS layer1
FROM AdminNavigation
WHERE id = 1

UNION

SELECT id AS layer2
FROM AdminNavigation
WHERE parentId = layer1

UNION

SELECT id AS layer3
FROM AdminNavigation
WHERE parentId = layer2

1 个答案:

答案 0 :(得分:1)

SELECT id AS layer1
FROM AdminNavigation
WHERE id = 1

UNION

SELECT id AS layer2
FROM AdminNavigation
WHERE parentId in(SELECT id
FROM AdminNavigation
WHERE id = 1
)

UNION

SELECT id AS layer3
FROM AdminNavigation
WHERE parentId in (
SELECT id
FROM AdminNavigation
WHERE parentId in(SELECT id
FROM AdminNavigation
WHERE id = 1
))

你不能简单地将内部查询作为简单的解决方案。 正如我在上面的查询中尝试过的那样。

希望这会有所帮助