Donatas
Donatas

Reputation: 41

MySQL - Using query results in another UNION query

How can I use results that I got from one query to another query that I'm joining with UNION? I'm trying to remove the root link and all of its children with a single use of SQL query. I could of course get the result, then with PHP make another query call, but prefer not to do it this way if possible. So far I've came to this code that doesn't work:

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

Upvotes: 0

Views: 165

Answers (1)

Sagar Gangwal
Sagar Gangwal

Reputation: 7937

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
))

You cant simply put inner query as simple resolution. As i had tried in above query.

Hope this will helps

Upvotes: 1

Related Questions