我正在尝试按车站出发时间订购火车时刻表,但是当火车经过午夜时出现问题。
下面是流入我的 mysql 数据库的数据示例。对于给定的火车时刻表,我有其基本路线和每个车站的基本出发时间(由 y 下 sch 标记)。我也可能时不时地收到给定火车时刻表的修改出发时间的更新。
就其本身而言,上述不是问题,因为我根据 timeref 对数据进行排序。然而,当火车经过午夜时,我的问题就来了。这是因为我收到的时间是纯 24 小时格式 - 即 01:00
Count Location Trainid Timeref Sche Update
1 London t256 22:10 y
2 Cricklewood t256 23:00 y
3 Luton t256 01:00 y
4 Leicester t256 02:00 y
5 Doncaster r659 06:00 y
6 Luton t256 01:10 y
7 Sheffield y783 05:00 y
我想显示如下数据(按该顺序)。 即按照原始时间表的顺序,但卢顿的时间更新为 01:10
London t256 22:10
Cricklewood t256 23:00
Luton t256 01:10
Leicester t256 02:00
我下面的代码工作正常,除非火车穿过午夜
$stmt3=$mysql_link->prepare("SELECT count,trainid,location,timeref,sche,update FROM (SELECT count,trainid,location,timeref,sche,update FROM darwinall WHERE trainid=:trainid ORDER BY count DESC) as temp GROUP BY location ORDER BY timeref ASC");
$stmt3->execute(array(':trainid'=>$trainid));
foreach($stmt3->fetchAll(PDO::FETCH_ASSOC) as $row3) {
echo result.................
}
所以而不是上面我得到以下结果,这是错误的
Luton t256 01:10
Leicester t256 02:00
London t256 22:10
Cricklewood t256 23:00
如果您想将午夜到早上 6 点之间的所有时间都放在其他时间之后,您可以执行以下操作:
ORDER BY timeref < '06:00', timeref
的一个技巧是检查当前时间,并首先对它之后的所有timeref
(即今天的火车)进行排序,然后才订购它之前的timeref
(即午夜后):
SELECT `count`, trainid, location, timeref, sche, `update`
FROM (SELECT `count`, trainid, location, timeref, sche, `update`
FROM darwinall
WHERE trainid=:trainid
ORDER BY `count` DESC) AS temp
GROUP BY location
ORDER BY timeref < CURRENT_TIME() ASC, timeref ASC
顺便说一句:count
和update
是保留词,你可能应该用反引号保护它们。