2

This is my table in which ID is table's primary key and SUPER_ID is used to manage hierarchy for example here, A is super user of B and B is super user of C while A is super-super user of C............ ID AND SUPER_ID MAY NOT NECESSARY BE SEQUENTIAL

Now question is when A is logged in, He can see details of his own and of Both B and C While B is logged in He can see details of His own and C only. And C can see his own details only.

I have written this query:

<?php
$sql="SELECT * from TABLE 
WHERE 
ID =:loginId OR                                                  
ID IN 
(
SELECT ID FROM TABLE 
WHERE SUPER_ID =:SuperId
)";
$stmt = $conn->prepare($sql);
$stmt->execute(array(':logedIn' => $_SESSION['sess_login_id'] , ':SuperId' => $_SESSION['sess_login_id']) );
?>

This query gives me results of A and B when i logged in as User A. What query should I write so that I can get results of A , B and C when I logged in as User A? Because A is a super user of B and super-super user of C. Please help. Thank You.

4
  • What happens when D comes in with C as its super? Commented Nov 21, 2015 at 9:02
  • Sorry i forget to mention, there is only three level hierarchy. So no scope of this to happen. Commented Nov 21, 2015 at 9:05
  • 2
    If SUPER_ID is always sequentially larger then the query can be simplified to (something like) select * from TABLE where ID = loginiId or SUPER_ID > SuperId Commented Nov 21, 2015 at 9:24
  • "SUPER_ID is always sequentially larger then the query" is not necessary. As it is editable from front end so any user is assigne under any user according to his role(there are three roles); Otherwise your solution is good! Commented Nov 21, 2015 at 9:43

2 Answers 2

2

If you have only three hierarchy levels, you can do it like this:

SELECT DISTINCT u.*
FROM user loggedin
LEFT JOIN user children      ON children.SUPR_ID      = loggedin.ID
LEFT JOIN user grandchildren ON grandchildren.SUPR_ID = children.ID
JOIN user u ON u.ID IN (loggedin.ID, children.ID, grandchildren.ID)
WHERE loggedin.ID = :loginId

http://sqlfiddle.com/#!9/3d93c/7

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you sir.. it works. if you can explain the query it will be grateful. I am not much expert in queries.
I renamed the table aliases. Hope that helps you to understand it. Replace SELECT DISTINCT u.* with SELECT * to see what MySQL have done in the background.
2

http://sqlfiddle.com/#!9/ad6a88/2/0 First get SUPR_ID from the user, then check which SUPR_ID is bigger or same.

SELECT * FROM `TABLE` WHERE SUPR_ID >= (SELECT SUPR_ID FROM `TABLE` WHERE ID=:loginId)

3 Comments

ID and SUPER_ID not necessary be sequential.
If SUPER_ID is not sequential. how do you determine which one is higher in hirarchy?.
There is no such specification but there is one field in same table which defines role, say P > Q > R. So cant have child, P can have child as Q roles user or R roles user and Q can only have R roles user as a child.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.