Optimizing query with sub-queries
Hi all,
Consider the following (My)SQL query I'm using:
Code:
SELECT *
FROM `PageRelations`
WHERE `ancestor` IN (
SELECT `descendant` FROM `PageRelations` WHERE `ancestor` = 1
)
OR `descendant` IN (
SELECT `descendant` FROM `PageRelations` WHERE `ancestor` = 1
)
The red marked sub-queries are identical, and I was wondering whether there is a way to only have the sub-query run once?
Thanks :wave:
Re: Optimizing query with sub-queries
See if this works
Code:
SELECT *
FROM PageRelations PR
LEFT JOIN PageRelations PR2 on PR2.Ancestor =1 and ((PR.Descendant = PR2.Descendant) or (PR.Ancestor = PR2.Descendant))
-tg