Answer the question
In order to leave comments, you need to log in
How can you optimize the following SQL query - displaying an element by the nearest parent?
The request works correctly, but is a "request within a request", which is not very good.
SELECT
t1.*
FROM (
SELECT
t1.`id` `user_id`
, t1.`parent_id` `owner_id`
, t1.`level`
, t2.`product_id`
, t2.`url`
FROM
`user_tree` t1
INNER JOIN
`product` t2 ON t2.`user_id` = t1.`parent_id`
WHERE
t1.`id` = 33 # (USER_ID HERE)
ORDER BY
t1.`level`
) t1
GROUP BY
t1.`product_id`
ORDER BY
t1.`product_id`
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question