I have a standard nested set model with each node having name, lft & rgt attributes.
I can find the superiors of a specific employee using:
SELECT P2.* FROM Personnel AS P1, Personnel AS P2
WHERE P1.lft BETWEEN P2.lft AND P2.rgt
AND P1.name = "Bob"
ORDER BY P2.lft
I can also find the level2 manager by adding limit & offset:
SELECT P2.* FROM Personnel AS P1, Personnel AS P2
WHERE P1.lft BETWEEN P2.lft AND P2.rgt
AND P1.name = "Bob"
ORDER BY P2.lft
LIMIT 2 OFFSET 1
In some situations I may have multiple employees called "Bob" working under different level2 managers. The level2 manager names are unique.
I need a query that returns all the employees called "Bob" for the level2 manager called "Susan".
Edit: My query needs to return only the "Bob that works under "Mary" as he is the only one with a level2 manager called "Susan".
Big Boss
/ \
Brian Susan
| |
Susan Mary
| |
Bob Bob
Putting the numbers in:
If we can specify that we know that its the "Susan" at
8
then:Otherwise, I think you need to know more about the record to specify which manager you're speaking of.
EDIT: Making the query a bit more complicated, I've calculated depth using suggestions from this article. The fiddle for this is working as well.