I have a table named member
with the structure below.
+----+-----------+-------------+
| id | parent_id | status |
+----+-----------+-------------+
| 1 | 0 | DISTRIBUTOR |
| 2 | 1 | DISTRIBUTOR |
| 3 | 2 | DISTRIBUTOR |
| 4 | 2 | RETAILER |
| 5 | 4 | RETAILER |
| 6 | 5 | DISTRIBUTOR |
+----+-----------+-------------+
now I want to find the parent id
. For example, if I select id
6 then it will show that the parent id of 6 is 5, but the twist is this: if I selected the id 6 since the status of it's parent 5 is RETAILER it will go up to check what is the parent_id of id 5 but since the parent _id of 5 which is id 4 is RETAILER it will go up again and check the parent_id of id 4 since the parent_id of 4 is 2 and status DISTRIBUTOR the conditon will stop and it will output that the parent_id of id 6 is the id 2
Edit (Query given by user in Comment section)
SELECT id, parent_id, status FROM member WHERE id=6.