// for alphabetical order: // $sortby = "`TableA`.`Name` ASC"; // for rank by points: // $sortby = " /*something here*/ DESC"; $query = "SELECT `TableA`.`Name`, `TableA`.`Reference1`, `TableA`.`Reference2`, `TableA`.`Reference3`, `TableA`.`Reference4`, `TableA`.`Reference5` FROM `TableA` ORDER BY $sortby"; $result = mysql_query($query, $link) or die(mysql_error($link)); while ($row = mysql_fetch_assoc($result)) { $total = 0; $query2d1 = "SELECT `TableB`.`Primary Key`, `TableB`.`Current Points` FROM `Table` WHERE `TableB`.`Primary Key` = '$row[`Reference1`]' $result2d1 = mysql_query($query2d1, $link2) or die(mysql_error($link2)); $row2d1 = mysql_fetch_assoc($result2d1)) $total = $total + $row2d1[`Current Points`]; $query2d2 = "SELECT `TableB`.`Primary Key`, `TableB`.`Current Points` FROM `Table` WHERE `TableB`.`Primary Key` = '$row[`Reference2`]' $result2d2 = mysql_query($query2d2, $link2) or die(mysql_error($link2)); $row2d2 = mysql_fetch_assoc($result2d2)) $total = $total + $row2d2[`Current Points`]; $query2d3 = "SELECT `TableB`.`Primary Key`, `TableB`.`Current Points` FROM `Table` WHERE `TableB`.`Primary Key` = '$row[`Reference3`]' $result2d3 = mysql_query($query2d3, $link2) or die(mysql_error($link2)); $row2d3 = mysql_fetch_assoc($result2d3)) $total = $total + $row2d3[`Current Points`]; $query2d4 = "SELECT `TableB`.`Primary Key`, `TableB`.`Current Points` FROM `Table` WHERE `TableB`.`Primary Key` = '$row[`Reference4`]' $result2d4 = mysql_query($query2d4, $link2) or die(mysql_error($link2)); $row2d4 = mysql_fetch_assoc($result2d4)) $total = $total + $row2d4[`Current Points`]; $query2d5 = "SELECT `TableB`.`Primary Key`, `TableB`.`Current Points` FROM `Table` WHERE `TableB`.`Primary Key` = '$row[`Reference5`]' $result2d5 = mysql_query($query2d5, $link2) or die(mysql_error($link2)); $row2d5 = mysql_fetch_assoc($result2d5)) $total = $total + $row2d5[`Current Points`]; echo "