I have a closure table HIERARCHY
ancestor | descendant | depth
100 | 100 | 0
2 | 100 | 1
3 | 100 | 2
4 | 100 | 3
and a joined table PROPERTIES
id | key | value
4 | action | DEFAULT
4 | duration | PT1H
100 | action | OVERRIDE
100 | duration | PT1M
I can get the whole sub-tree using the query
SELECT id, key, value
FROM hierarchy, properties
WHERE id = ancestor
AND descendant = 100
ORDER BY depth
What would be the simplest/fastest way to get the lowest hierarchy member?, ie. having min(depth)
100 | action | OVERRIDE
100 | duration | PT1M
I still need to keep the hierarchy, meaning that if 100
isn't found in the query, 4
would show up.
In other words, I'm trying to find all the rows of the tree member with the lowest depth possible, possibly without duplicating the query in the WHERE
clause
The database is the currently full-released mysql, ie. 5.7