MySQL PHP: How can I combine this nested WHILE loop into a single query? -


i have following code works, know it's not efficient , consolidate single sql query. should bother? , if so, how?

function get_rnds_by_judge($judge_id) { global $connection; $sql = "select *         round_user         left join round         on round_user.rndid = round.rndid         round_user.userid = '$judge_id'         "; $rnd_set = mysql_query($sql, $connection); confirm_query($rnd_set); return $rnd_set; }  function get_users_by_round_and_role($round_id, $role_id) {     global $connection;     $sql = "select *             user             left join round_user             on user.userid = round_user.userid             round_user.rndid = '$round_id' , user.userroleid = '$role_id'             ";     $rnd_set = mysql_query($sql, $connection);     confirm_query($rnd_set);     return $rnd_set; }  $rnd_set = get_rnds_by_judge($_session[userid]); while($rnd_row = mysql_fetch_array($rnd_set)){     echo "<li data-role=\"list-divider\">";     echo $rnd_row[rndtitle];     echo "</li>";     $rs_set = get_users_by_round_and_role($rnd_row[rndid], 7);     while($rs_row = mysql_fetch_array($rs_set)){         echo "<li>";         echo "<a href=\"#\">{$rs_row[userfirstname]}</a>";         echo "</li>";     } } 

the part trip on how take rndid , userid joining round_user table , go data 2 different tables bring together.

it's hard tell without seeing table structures , sample data, looking this

select u.*   user u left join         round_user ru on u.userid = ru.userid  ru.rndid in (        select ru2.rndid          round_user ru2 left join                round r on ru2.rndid = r.rndid         ru2.userid = '$judge_id')     , u.userroleid = 7 

the query has not been tested.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -