简体   繁体   English

按字母顺序排列的MYSQL排序组中的最后记录

[英]Alphabetical MYSQL Sort of Last Record In Group

I am trying to follow the answer given in " Retrieving the last record in each group " but I have a problem 我正在尝试遵循“ 检索每个组中的最后一个记录 ”中给出的答案,但是我遇到了问题

My problem is that I am echoing both Y78430's (ie count 1 and 3) when I only want to echo count 3 我的问题是,当我只想回显计数3时,我同时回显了两个Y78430(即计数1和3)。

I am trying to pick the last record of groups of data where the last record is the lower alphabetical letter. 我正在尝试选择数据组的最后一条记录,其中最后一条记录是小写字母。

Example of my data here (Table is 'schedulelocation'):- 我在这里的数据示例(表为“ schedulelocation”):-

 Count       cif_train_uid       cif_stp_indicator        Other Data

   1          Y78430                   p                    zzzzzzz
   2          Z45012                   p                    fffffff
   3          Y78430                   o                    sssssss

In the above data there are 2 X Y78430. 在以上数据中有2 X Y78430。 I would like to echo only one of these. 我只想回声其中之一。 The one with a cif_stp_indicator of o - ie its lower in the alphabet than the ' p ' 一个cif_stp_indicator为o的字母-即它的字母比' p '低

Here is my 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 < 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 "</b>";
 echo "</b>";


 }    

             }          

Any help greatly appreciated. 任何帮助,不胜感激。 Thanks 谢谢

You need to use an aggregate query to find the appropriate row to display, and join that to your basic information. 您需要使用汇总查询来找到要显示的适当行,并将其与您的基本信息联系起来。

Your aggregate query is this: 您的汇总查询是这样的:

              SELECT cif_train_uid, 
                     MIN(cif_stp_indicator) as cif_stp_indicator
                FROM schedulelocation
            GROUP BY cif_train_uid

It returns a list of pairs of (train, indicator) where the indicator is the lowest (lexically first) one for the train. 它返回(火车,指示符)对的列表,其中指示符是火车中最低的(从词法上来说是第一个)。

Then you INNER JOIN it into the rest of your query like this. 然后,您可以像这样将其INNER JOIN插入查询的其余部分。

     SELECT s1.cif_train_uid,s1.cif_stp_indicator,s1.schedule_start_date
       FROM (   SELECT cif_train_uid, MIN(cif_stp_indicator) as cif_stp_indicator
                  FROM schedulelocation
              GROUP BY cif_train_uid
            ) AS m 
 INNER JOIN schedulelocation s1
         ON m.cif_train_uid=s1.cif_train_uid 
        AND m.cif_stp_indicator = s1.cif_stp_indicator 
  LEFT JOIN schedulelocation s2
         ON s1.cif_train_uid 
        AND s1.cif_stp_indicator < s2.cif_stp_indicator)
      WHERE s2.cif_stp_indicator is Null    /* IS THIS CORRECT ?? */
        AND s1.cif_train_uid='Y78430'

Note I'm not 100% sure what's going on with your self-join operation. 请注意,我不确定100%进行自联接操作。 I think your first WHERE clause eliminates all the rows where the self-join actually found something. 我认为您的第一个WHERE子句消除了自我联接实际发现某物的所有行。 I don't get it. 我不明白

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM