title: Alphabetical MYSQL Sort of Last Record In Group
body: <p>I am trying to follow the answer given in " <a href="https://stackoverflow.com/questions/1313120/retrieving-the-last-record-in-each-group?lq=1">Retrieving the last record in each group</a> " but I have a problem</p>
  
  <p>My problem is that I am echoing both Y78430's (ie count 1 and 3) when I only want to echo count 3</p>
  
  <p>I am trying to pick the last record of groups of data where the last record is the lower alphabetical letter.</p>
  
  <p>Example of my data here (Table is 'schedulelocation'):-</p>
  
  <pre><code> Count       cif_train_uid       cif_stp_indicator        Other Data
  
     1          Y78430                   p                    zzzzzzz
     2          Z45012                   p                    fffffff
     3          Y78430                   o                    sssssss
  </code></pre>
  
  <p>In the above data there are 2 X Y78430. I would like to echo only one of these. The one with a cif_stp_indicator of <strong>o</strong> - ie its lower in the alphabet than the '<strong>p</strong>'</p>
  
  <p>Here is my code :-</p>
  
  <pre><code> $b="SELECT s1.cif_train_uid,s1.cif_stp_indicator,s1.schedule_start_date
   FROM schedulelocation s1
   LEFT JOIN schedulelocation s2
    ON (s1.cif_train_uid AND s1.cif_stp_indicator &lt; s2.cif_stp_indicator)
   WHERE s2.cif_stp_indicator is Null AND s1.cif_train_uid='Y78430' ";             
  
   $l=mysqli_query($mysql_link,$b);   
  
   if ($l) {
  
   while($berths=mysqli_fetch_array($l,MYSQLI_ASSOC))
   {  
  
   echo $berths['cif_train_uid']; 
   echo $berths['cif_stp_indicator'];
   echo $berths['schedule_start_date'];
   echo "&lt;/b&gt;";
   echo "&lt;/b&gt;";
  
  
   }    
  
               }          
  </code></pre>
  
  <p>Any help greatly appreciated. Thanks    </p>