I have a table that lists a hierarchy of team members for an MLM. I am trying to figure out how to query the table most efficiently in MySQL so I can output the results in PHP.
I have learned in research that nested queries or nested queries in PHP while loops are not ideal, yet when I look at MySQL joins, I don't see how to access the information correctly.
Table Values (Table Name is MLM):
ID Upline FName
1 1 Joe (he is at the top of the hierarchy)
2 1 Jane (Jane is directly under Joe)
3 2 Jack
4 3 Jill
To find out the names of all these people reporting under Joe?
Select FName from MLM where Upline = 1;
and
Select FName from MLM where Upline in (Select FName from MLM where Upline = 1);
and
Select FName from MLM where Upline in (Select FName from MLM where Upline in (Select FName from MLM where Upline = 1));
This isn't good to start, and not knowing how many levels will be under Joe, it isn't scalable.
Can you please let me know how I can best query this information or combine a PHP mysql_fetch_array in a while loop to find and echo all the values?