title: Sorting table data from different queries in PHP/MySql
body: <p>I am really confused as I am a (poorly) self-taught amateur. I am building a statistics site for a league and I managed to create a table that takes data from several tables in a database and collates it together. 
  The current "columns" in the table are Driver Name, total number of races, total wins and podium placements. Driver Name is the only "straightforward" set of data I have - all the rest is obtained through calculations and, as such, requires the initial data regarding the names.</p>
  
  <p>At the moment, I have the table showing the correct data in drivers' alphabetical order. How can I do it so that it can be sorted (even in a completely different page) by the other three fields?</p>
  
  <p>The current table can be seen here: <a href="http://www.gpvwc.com/en/services/database/driver/" rel="nofollow">http://www.gpvwc.com/en/services/database/driver/</a></p>
  
  <p>Here is the code I wrote to have that appear:</p>
  
  <pre><code>$data90=mysql_query("SELECT DISTINCT competition FROM DCO_results ORDER BY competition ASC", $CONNECTW);
          while($row=mysql_fetch_row($data90)) {
  
          $competitionnum="$row[0]";
  
          $data80=mysql_query("SELECT competition FROM DCO_competitions WHERE id='$competitionnum'", $CONNECTW);
          while($row=mysql_fetch_row($data80)) {
  
          $compstring="$row[0]";  
  
              echo "&lt;span class='footer'&gt;&lt;a href='#$compstring'&gt;$compstring&lt;/a&gt; | &lt;/span&gt;";
          }}
  
  
  echo "&lt;p&gt;&amp;nbsp;&lt;/p&gt;";
  
  $data70=mysql_query("SELECT DISTINCT competition FROM DCO_results ORDER BY competition ASC", $CONNECTW);
          while($row=mysql_fetch_row($data70)) {
  
          $compnum3="$row[0]";
  
  
  $data60=mysql_query("SELECT competition FROM DCO_competitions WHERE id='$compnum3'", $CONNECTW);
          while($row=mysql_fetch_row($data60)) {
  
          $compstring3="$row[0]";         
  
  echo "&lt;a name='$compstring3' id='$compstring3'&gt;&lt;/a&gt;&lt;h2&gt;$compstring3&lt;/h2&gt;";          
  
  if ($compnum3=="1") {
  
  $sql = '(SELECT DISTINCT driver1 FROM DCO_results WHERE competition="1"  AND driver1 IS NOT NULL) UNION (SELECT DISTINCT driver2 FROM DCO_results WHERE competition="1"  AND driver2 IS NOT NULL) UNION (SELECT DISTINCT driver3 FROM DCO_results WHERE competition="1"  AND driver3 IS NOT NULL) UNION (SELECT DISTINCT driver4 FROM DCO_results WHERE competition="1"  AND driver4 IS NOT NULL) UNION (SELECT DISTINCT driver5 FROM DCO_results WHERE competition="1"  AND driver5 IS NOT NULL) UNION (SELECT DISTINCT driver6 FROM DCO_results WHERE competition="1"  AND driver6 IS NOT NULL) UNION (SELECT DISTINCT driver7 FROM DCO_results WHERE competition="1"  AND driver7 IS NOT NULL) UNION (SELECT DISTINCT driver8 FROM DCO_results WHERE competition="1"  AND driver8 IS NOT NULL) UNION (SELECT DISTINCT driver9 FROM DCO_results WHERE competition="1"  AND driver9 IS NOT NULL) UNION (SELECT DISTINCT driver10 FROM DCO_results WHERE competition="1"  AND driver10 IS NOT NULL) UNION (SELECT DISTINCT driver11 FROM DCO_results WHERE competition="1"  AND driver11 IS NOT NULL) UNION (SELECT DISTINCT driver12 FROM DCO_results WHERE competition="1"  AND driver12 IS NOT NULL) UNION (SELECT DISTINCT driver13 FROM DCO_results WHERE competition="1"  AND driver13 IS NOT NULL) UNION (SELECT DISTINCT driver14 FROM DCO_results WHERE competition="1"  AND driver14 IS NOT NULL) UNION (SELECT DISTINCT driver15 FROM DCO_results WHERE competition="1"  AND driver15 IS NOT NULL) UNION (SELECT DISTINCT driver16 FROM DCO_results WHERE competition="1"  AND driver16 IS NOT NULL) UNION (SELECT DISTINCT driver17 FROM DCO_results WHERE competition="1"  AND driver17 IS NOT NULL) UNION (SELECT DISTINCT driver18 FROM DCO_results WHERE competition="1"  AND driver18 IS NOT NULL) UNION (SELECT DISTINCT driver19 FROM DCO_results WHERE competition="1"  AND driver19 IS NOT NULL) UNION (SELECT DISTINCT driver20 FROM DCO_results WHERE competition="1"  AND driver20 IS NOT NULL) UNION (SELECT DISTINCT driver21 FROM DCO_results WHERE competition="1"  AND driver21 IS NOT NULL) UNION (SELECT DISTINCT driver22 FROM DCO_results WHERE competition="1"  AND driver22 IS NOT NULL) UNION (SELECT DISTINCT driver23 FROM DCO_results WHERE competition="1"  AND driver23 IS NOT NULL) UNION (SELECT DISTINCT driver24 FROM DCO_results WHERE competition="1"  AND driver24 IS NOT NULL) UNION (SELECT DISTINCT driver25 FROM DCO_results WHERE competition="1"  AND driver25 IS NOT NULL) UNION (SELECT DISTINCT driver26 FROM DCO_results WHERE competition="1"  AND driver26 IS NOT NULL) UNION (SELECT DISTINCT driver27 FROM DCO_results WHERE competition="1"  AND driver27 IS NOT NULL) UNION (SELECT DISTINCT driver28 FROM DCO_results WHERE competition="1"  AND driver28 IS NOT NULL) UNION (SELECT DISTINCT driver29 FROM DCO_results WHERE competition="1"  AND driver29 IS NOT NULL) UNION (SELECT DISTINCT driver30 FROM DCO_results WHERE competition="1"  AND driver30 IS NOT NULL) UNION (SELECT DISTINCT driver31 FROM DCO_results WHERE competition="1"  AND driver31 IS NOT NULL) UNION (SELECT DISTINCT driver32 FROM DCO_results WHERE competition="1"  AND driver32 IS NOT NULL) UNION (SELECT DISTINCT driver33 FROM DCO_results WHERE competition="1"  AND driver33 IS NOT NULL) UNION (SELECT DISTINCT driver34 FROM DCO_results WHERE competition="1"  AND driver34 IS NOT NULL) UNION (SELECT DISTINCT driver35 FROM DCO_results WHERE competition="1"  AND driver35 IS NOT NULL) UNION (SELECT DISTINCT driver36 FROM DCO_results WHERE competition="1"  AND driver36 IS NOT NULL) UNION (SELECT DISTINCT driver37 FROM DCO_results WHERE competition="1"  AND driver37 IS NOT NULL) UNION (SELECT DISTINCT driver38 FROM DCO_results WHERE competition="1"  AND driver38 IS NOT NULL) UNION (SELECT DISTINCT driver39 FROM DCO_results WHERE competition="1"  AND driver39 IS NOT NULL) UNION (SELECT DISTINCT driver40 FROM DCO_results WHERE competition="1"  AND driver40 IS NOT NULL) UNION (SELECT DISTINCT driver41 FROM DCO_results WHERE competition="1"  AND driver41 IS NOT NULL) UNION (SELECT DISTINCT driver42 FROM DCO_results WHERE competition="1"  AND driver42 IS NOT NULL) UNION (SELECT DISTINCT driver43 FROM DCO_results WHERE competition="1"  AND driver43 IS NOT NULL) UNION (SELECT DISTINCT driver44 FROM DCO_results WHERE competition="1"  AND driver44 IS NOT NULL) UNION (SELECT DISTINCT driver45 FROM DCO_results WHERE competition="1"  AND driver45 IS NOT NULL) UNION (SELECT DISTINCT driver46 FROM DCO_results WHERE competition="1"  AND driver46 IS NOT NULL) UNION (SELECT DISTINCT driver47 FROM DCO_results WHERE competition="1"  AND driver47 IS NOT NULL) UNION (SELECT DISTINCT driver48 FROM DCO_results WHERE competition="1"  AND driver48 IS NOT NULL) ORDER BY driver1 ASC';  
  
  $result = mysql_query($sql); 
  
  $totaldrivers=mysql_num_rows($result);
  
  echo "&lt;p&gt;&amp;nbsp;&lt;/p&gt;";
  echo "&lt;p&gt;A total of $totaldrivers drivers took part in the GPVWC $compstring3.&lt;/p&gt;";
  
  echo "&lt;table width='100% border='0' cellspacing='0' cellpadding='0' align='left'&gt;";
  
      echo "&lt;tr&gt;
  
                  &lt;th width='250' class='bg_th'&gt;&lt;span class='footer'&gt;Driver&lt;/span&gt;&lt;/th&gt;
                  &lt;th width='50' class='bg_th' align='center'&gt;&lt;span class='footer'&gt;&lt;a href='$RKP/en/services/database/driver/sort/?target=Races'&gt;Races&lt;/a&gt;&lt;/span&gt;&lt;/th&gt;
                  &lt;th width='50' class='bg_th' align='center'&gt;&lt;span class='footer'&gt;&lt;a href='$RKP/en/services/database/driver/sort/?target=Wins'&gt;Wins&lt;/a&gt;&lt;/span&gt;&lt;/th&gt;
                  &lt;th width='50' class='bg_th' align='center'&gt;&lt;span class='footer'&gt;&lt;a href='$RKP/en/services/database/driver/sort/?target=Podiums'&gt;Podiums&lt;/a&gt;&lt;/span&gt;&lt;/th&gt;
                  &lt;th width='50' class='bg_th' align='center'&gt;&lt;span class='footer'&gt;Points&lt;/span&gt;&lt;/th&gt;
                  &lt;th width='50' class='bg_th' align='center'&gt;&lt;span class='footer'&gt;Pts Finish&lt;/span&gt;&lt;/th&gt;
              &lt;/tr&gt;";
  
  while($get_info = mysql_fetch_row($result)){
  
  $driversnames="$get_info[0]";
  
  $data55=mysql_query("SELECT id FROM DCO_results WHERE competition='$compnum3' AND session='R' AND (driver1='$driversnames' OR driver2='$driversnames' OR driver3='$driversnames' OR driver4='$driversnames' OR driver5='$driversnames' OR driver6='$driversnames' OR driver7='$driversnames' OR driver8='$driversnames' OR driver9='$driversnames' OR driver10='$driversnames' OR driver11='$driversnames' OR driver12='$driversnames' OR driver13='$driversnames' OR driver14='$driversnames' OR driver15='$driversnames' OR driver16='$driversnames' OR driver17='$driversnames' OR driver18='$driversnames' OR driver19='$driversnames'OR driver20='$driversnames' OR driver21='$driversnames' OR driver22='$driversnames' OR driver23='$driversnames' OR driver24='$driversnames' OR driver25='$driversnames' OR driver26='$driversnames' OR driver27='$driversnames' OR driver28='$driversnames' OR driver29='$driversnames' OR driver30='$driversnames' OR driver31='$driversnames' OR driver32='$driversnames' OR driver33='$driversnames' OR driver34='$driversnames' OR driver35='$driversnames' OR driver36='$driversnames' OR driver37='$driversnames' OR driver38='$driversnames' OR driver39='$driversnames' OR driver40='$driversnames' OR driver41='$driversnames' OR driver42='$driversnames' OR driver43='$driversnames' OR driver44='$driversnames' OR driver45='$driversnames' OR driver46='$driversnames' OR driver47='$driversnames' OR driver48='$driversnames') ORDER BY id DESC", $CONNECTW);
  $totalraceslist=mysql_num_rows($data55);
  
  $data66=mysql_query("SELECT id FROM DCO_results WHERE competition='$compnum3' AND session='R' AND (driver1='$driversnames') ORDER BY id DESC", $CONNECTW);
  $totalwinslist=mysql_num_rows($data66);
  
  $data77=mysql_query("SELECT id FROM DCO_results WHERE competition='$compnum3' AND session='R' AND (driver1='$driversnames' OR driver2='$driversnames' OR driver3='$driversnames') ORDER BY id DESC", $CONNECTW);
  $totalpodiumslist=mysql_num_rows($data77);
  
  
      echo "&lt;tr&gt;
  
                  &lt;td width='250' class='bg_th'&gt;&lt;span class='footer'&gt;&lt;a href='$RKP/en/services/database/driver/?target=$driversnames'&gt;$driversnames&lt;/a&gt;&lt;/span&gt;&lt;/td&gt;
                  &lt;td width='50' class='bg_th' align='center'&gt;&lt;span class='footer'&gt;$totalraceslist&lt;/span&gt;&lt;/td&gt;
                  &lt;td width='50' class='bg_th' align='center'&gt;&lt;span class='footer'&gt;$totalwinslist&lt;/span&gt;&lt;/td&gt;
                  &lt;td width='50' class='bg_th' align='center'&gt;&lt;span class='footer'&gt;$totalpodiumslist&lt;/span&gt;&lt;/td&gt;
                  &lt;td width='50' class='bg_th' align='center'&gt;&lt;span class='footer'&gt;N/A&lt;/span&gt;&lt;/td&gt;
                  &lt;td width='50' class='bg_th' align='center'&gt;&lt;span class='footer'&gt;N/A&lt;/span&gt;&lt;/td&gt;                    
              &lt;/tr&gt;"; }}
  
  if ($compnum3=="2") {
  
  $sql = '(SELECT DISTINCT driver1 FROM DCO_results WHERE competition="2"  AND driver1 IS NOT NULL) UNION (SELECT DISTINCT driver2 FROM DCO_results WHERE competition="2"  AND driver2 IS NOT NULL) UNION (SELECT DISTINCT driver3 FROM DCO_results WHERE competition="2"  AND driver3 IS NOT NULL) UNION (SELECT DISTINCT driver4 FROM DCO_results WHERE competition="2"  AND driver4 IS NOT NULL) UNION (SELECT DISTINCT driver5 FROM DCO_results WHERE competition="2"  AND driver5 IS NOT NULL) UNION (SELECT DISTINCT driver6 FROM DCO_results WHERE competition="2"  AND driver6 IS NOT NULL) UNION (SELECT DISTINCT driver7 FROM DCO_results WHERE competition="2"  AND driver7 IS NOT NULL) UNION (SELECT DISTINCT driver8 FROM DCO_results WHERE competition="2"  AND driver8 IS NOT NULL) UNION (SELECT DISTINCT driver9 FROM DCO_results WHERE competition="2"  AND driver9 IS NOT NULL) UNION (SELECT DISTINCT driver10 FROM DCO_results WHERE competition="2"  AND driver10 IS NOT NULL) UNION (SELECT DISTINCT driver11 FROM DCO_results WHERE competition="2"  AND driver11 IS NOT NULL) UNION (SELECT DISTINCT driver12 FROM DCO_results WHERE competition="2"  AND driver12 IS NOT NULL) UNION (SELECT DISTINCT driver13 FROM DCO_results WHERE competition="2"  AND driver13 IS NOT NULL) UNION (SELECT DISTINCT driver14 FROM DCO_results WHERE competition="2"  AND driver14 IS NOT NULL) UNION (SELECT DISTINCT driver15 FROM DCO_results WHERE competition="2"  AND driver15 IS NOT NULL) UNION (SELECT DISTINCT driver16 FROM DCO_results WHERE competition="2"  AND driver16 IS NOT NULL) UNION (SELECT DISTINCT driver17 FROM DCO_results WHERE competition="2"  AND driver17 IS NOT NULL) UNION (SELECT DISTINCT driver18 FROM DCO_results WHERE competition="2"  AND driver18 IS NOT NULL) UNION (SELECT DISTINCT driver19 FROM DCO_results WHERE competition="2"  AND driver19 IS NOT NULL) UNION (SELECT DISTINCT driver20 FROM DCO_results WHERE competition="2"  AND driver20 IS NOT NULL) UNION (SELECT DISTINCT driver21 FROM DCO_results WHERE competition="2"  AND driver21 IS NOT NULL) UNION (SELECT DISTINCT driver22 FROM DCO_results WHERE competition="2"  AND driver22 IS NOT NULL) UNION (SELECT DISTINCT driver23 FROM DCO_results WHERE competition="2"  AND driver23 IS NOT NULL) UNION (SELECT DISTINCT driver24 FROM DCO_results WHERE competition="2"  AND driver24 IS NOT NULL) UNION (SELECT DISTINCT driver25 FROM DCO_results WHERE competition="2"  AND driver25 IS NOT NULL) UNION (SELECT DISTINCT driver26 FROM DCO_results WHERE competition="2"  AND driver26 IS NOT NULL) UNION (SELECT DISTINCT driver27 FROM DCO_results WHERE competition="2"  AND driver27 IS NOT NULL) UNION (SELECT DISTINCT driver28 FROM DCO_results WHERE competition="2"  AND driver28 IS NOT NULL) UNION (SELECT DISTINCT driver29 FROM DCO_results WHERE competition="2"  AND driver29 IS NOT NULL) UNION (SELECT DISTINCT driver30 FROM DCO_results WHERE competition="2"  AND driver30 IS NOT NULL) UNION (SELECT DISTINCT driver31 FROM DCO_results WHERE competition="2"  AND driver31 IS NOT NULL) UNION (SELECT DISTINCT driver32 FROM DCO_results WHERE competition="2"  AND driver32 IS NOT NULL) UNION (SELECT DISTINCT driver33 FROM DCO_results WHERE competition="2"  AND driver33 IS NOT NULL) UNION (SELECT DISTINCT driver34 FROM DCO_results WHERE competition="2"  AND driver34 IS NOT NULL) UNION (SELECT DISTINCT driver35 FROM DCO_results WHERE competition="2"  AND driver35 IS NOT NULL) UNION (SELECT DISTINCT driver36 FROM DCO_results WHERE competition="2"  AND driver36 IS NOT NULL) UNION (SELECT DISTINCT driver37 FROM DCO_results WHERE competition="2"  AND driver37 IS NOT NULL) UNION (SELECT DISTINCT driver38 FROM DCO_results WHERE competition="2"  AND driver38 IS NOT NULL) UNION (SELECT DISTINCT driver39 FROM DCO_results WHERE competition="2"  AND driver39 IS NOT NULL) UNION (SELECT DISTINCT driver40 FROM DCO_results WHERE competition="2"  AND driver40 IS NOT NULL) UNION (SELECT DISTINCT driver41 FROM DCO_results WHERE competition="2"  AND driver41 IS NOT NULL) UNION (SELECT DISTINCT driver42 FROM DCO_results WHERE competition="2"  AND driver42 IS NOT NULL) UNION (SELECT DISTINCT driver43 FROM DCO_results WHERE competition="2"  AND driver43 IS NOT NULL) UNION (SELECT DISTINCT driver44 FROM DCO_results WHERE competition="2"  AND driver44 IS NOT NULL) UNION (SELECT DISTINCT driver45 FROM DCO_results WHERE competition="2"  AND driver45 IS NOT NULL) UNION (SELECT DISTINCT driver46 FROM DCO_results WHERE competition="2"  AND driver46 IS NOT NULL) UNION (SELECT DISTINCT driver47 FROM DCO_results WHERE competition="2"  AND driver47 IS NOT NULL) UNION (SELECT DISTINCT driver48 FROM DCO_results WHERE competition="2"  AND driver48 IS NOT NULL) ORDER BY driver1 ASC';  
  
  $result = mysql_query($sql); 
  
  $totaldrivers=mysql_num_rows($result);
  
  echo "&lt;p&gt;&amp;nbsp;&lt;/p&gt;";
  echo "&lt;p&gt;A total of $totaldrivers drivers took part in the GPVWC $compstring3.&lt;/p&gt;";
  
  echo "&lt;table width='100% border='0' cellspacing='0' cellpadding='0' align='left'&gt;";
  
      echo "&lt;tr&gt;
  
                  &lt;th width='250' class='bg_th'&gt;&lt;span class='footer'&gt;Driver&lt;/span&gt;&lt;/th&gt;
                  &lt;th width='50' class='bg_th' align='center'&gt;&lt;span class='footer'&gt;Races&lt;/span&gt;&lt;/th&gt;
                  &lt;th width='50' class='bg_th' align='center'&gt;&lt;span class='footer'&gt;Wins&lt;/span&gt;&lt;/th&gt;
                  &lt;th width='50' class='bg_th' align='center'&gt;&lt;span class='footer'&gt;Podiums&lt;/span&gt;&lt;/th&gt;
                  &lt;th width='50' class='bg_th' align='center'&gt;&lt;span class='footer'&gt;Points&lt;/span&gt;&lt;/th&gt;
                  &lt;th width='50' class='bg_th' align='center'&gt;&lt;span class='footer'&gt;Pts Finish&lt;/span&gt;&lt;/th&gt;
              &lt;/tr&gt;";
  
  while($get_info = mysql_fetch_row($result)){
  
  $driversnames="$get_info[0]";
  
  $data55=mysql_query("SELECT id FROM DCO_results WHERE competition='$compnum3' AND session='R' AND (driver1='$driversnames' OR driver2='$driversnames' OR driver3='$driversnames' OR driver4='$driversnames' OR driver5='$driversnames' OR driver6='$driversnames' OR driver7='$driversnames' OR driver8='$driversnames' OR driver9='$driversnames' OR driver10='$driversnames' OR driver11='$driversnames' OR driver12='$driversnames' OR driver13='$driversnames' OR driver14='$driversnames' OR driver15='$driversnames' OR driver16='$driversnames' OR driver17='$driversnames' OR driver18='$driversnames' OR driver19='$driversnames'OR driver20='$driversnames' OR driver21='$driversnames' OR driver22='$driversnames' OR driver23='$driversnames' OR driver24='$driversnames' OR driver25='$driversnames' OR driver26='$driversnames' OR driver27='$driversnames' OR driver28='$driversnames' OR driver29='$driversnames' OR driver30='$driversnames' OR driver31='$driversnames' OR driver32='$driversnames' OR driver33='$driversnames' OR driver34='$driversnames' OR driver35='$driversnames' OR driver36='$driversnames' OR driver37='$driversnames' OR driver38='$driversnames' OR driver39='$driversnames' OR driver40='$driversnames' OR driver41='$driversnames' OR driver42='$driversnames' OR driver43='$driversnames' OR driver44='$driversnames' OR driver45='$driversnames' OR driver46='$driversnames' OR driver47='$driversnames' OR driver48='$driversnames') ORDER BY id DESC", $CONNECTW);
  $totalraceslist=mysql_num_rows($data55);
  
  $data66=mysql_query("SELECT id FROM DCO_results WHERE competition='$compnum3' AND session='R' AND (driver1='$driversnames') ORDER BY id DESC", $CONNECTW);
  $totalwinslist=mysql_num_rows($data66);
  
  $data77=mysql_query("SELECT id FROM DCO_results WHERE competition='$compnum3' AND session='R' AND (driver1='$driversnames' OR driver2='$driversnames' OR driver3='$driversnames') ORDER BY id DESC", $CONNECTW);
  $totalpodiumslist=mysql_num_rows($data77);
  
  
      echo "&lt;tr&gt;
  
                  &lt;td width='250' class='bg_th'&gt;&lt;span class='footer'&gt;&lt;a href='$RKP/en/services/database/driver/?target=$driversnames'&gt;$driversnames&lt;/a&gt;&lt;/span&gt;&lt;/td&gt;
                  &lt;td width='50' class='bg_th' align='center'&gt;&lt;span class='footer'&gt;$totalraceslist&lt;/span&gt;&lt;/td&gt;
                  &lt;td width='50' class='bg_th' align='center'&gt;&lt;span class='footer'&gt;$totalwinslist&lt;/span&gt;&lt;/td&gt;
                  &lt;td width='50' class='bg_th' align='center'&gt;&lt;span class='footer'&gt;$totalpodiumslist&lt;/span&gt;&lt;/td&gt;
                  &lt;td width='50' class='bg_th' align='center'&gt;&lt;span class='footer'&gt;N/A&lt;/span&gt;&lt;/td&gt;
                  &lt;td width='50' class='bg_th' align='center'&gt;&lt;span class='footer'&gt;N/A&lt;/span&gt;&lt;/td&gt;                    
              &lt;/tr&gt;";     }}
  
      echo "&lt;/table&gt;"; 
      echo "&lt;p&gt;&amp;nbsp;&lt;/p&gt;";}
  
  
  
  
      echo "&lt;/table&gt;"; 
      echo "&lt;p&gt;&amp;nbsp;&lt;/p&gt;";   }   
  </code></pre>
  
  <p>I don't even know if that is comprehensible (I can say I am not exactly canonical as an editor) but I've been losing days (and my sleep) on this and I finally decided to ask for help.</p>