Mysql
mysql> select origin_id, destination_id,travel_date,departure_time,available_seats from `schedules` WHERE travel_date = '2014-08-12' and origin_id = '134' and destination_id = '251' ORDER BY STR_TO_DATE(departure_time,'%h:%i%p') limit 10;
+-----------+----------------+-------------+----------------+-----------------+
| origin_id | destination_id | travel_date | departure_time | available_seats |
+-----------+----------------+-------------+----------------+-----------------+
| 134 | 251 | 2014-08-12 | 06:00 AM | 34 |
| 134 | 251 | 2014-08-12 | 07:00 AM | 43 |
| 134 | 251 | 2014-08-12 | 07:30 AM | 0 |
| 134 | 251 | 2014-08-12 | 07:30 AM | 34 |
| 134 | 251 | 2014-08-12 | 08:30 AM | 0 |
| 134 | 251 | 2014-08-12 | 08:30 AM | 29 |
| 134 | 251 | 2014-08-12 | 10:15 AM | 36 |
| 134 | 251 | 2014-08-12 | 10:15 AM | 0 |
| 134 | 251 | 2014-08-12 | 10:30 AM | 29 |
| 134 | 251 | 2014-08-12 | 11:00 AM | 0 |
+-----------+----------------+-------------+----------------+-----------------+
10 rows in set (0.04 sec)
第一个查询:在此查询中,我按出发时间发出订单。 但是我希望所有记录中的available_seats 0都应该以departure_time的顺序排在最后。
必填项 ...... ??
+-----------+----------------+-------------+----------------+-----------------+
| origin_id | destination_id | travel_date | departure_time | available_seats |
+-----------+----------------+-------------+----------------+-----------------+
| 134 | 251 | 2014-08-12 | 06:00 AM | 34 |
| 134 | 251 | 2014-08-12 | 07:00 AM | 43 |
| 134 | 251 | 2014-08-12 | 07:30 AM | 34 |
| 134 | 251 | 2014-08-12 | 08:30 AM | 29 |
| 134 | 251 | 2014-08-12 | 10:15 AM | 36 |
| 134 | 251 | 2014-08-12 | 10:30 AM | 29 |
| 134 | 251 | 2014-08-12 | 07:30 AM | 0 |
| 134 | 251 | 2014-08-12 | 08:30 AM | 0 |
| 134 | 251 | 2014-08-12 | 10:15 AM | 0 |
| 134 | 251 | 2014-08-12 | 11:00 AM | 0 |
+-----------+----------------+-------------+----------------+-----------------+
是否可以使用mysql查询。或与其他方式???
答案 0 :(得分:0)
您可以先使用where和order by语句对可用席位的非零值的出发时间进行排序,然后可以按顺序应用union,以按时间递增的顺序附加零柱状可用席位。代码如下:
select origin_id, destination_id,travel_date,departure_time,available_seats from `schedules` WHERE travel_date = '2014-08-12' and origin_id = '134' and destination_id = '251' ORDER BY STR_TO_DATE(departure_time,'%h:%i%p') limit 10 and available_seats <> 0
union
select origin_id, destination_id,travel_date,departure_time,available_seats from `schedules` WHERE travel_date = '2014-08-12' and origin_id = '134' and destination_id = '251' ORDER BY STR_TO_DATE(departure_time,'%h:%i%p') limit 10 where available_seats = 0;
简而言之,添加available_seats&lt;&gt;在联盟的第一部分0, 然后 在union之后应用available_seats = 0。
答案 1 :(得分:0)
使用CASE子句:
ORDER BY CASE
WHEN available_seats=0 THEN '99:99'
else STR_TO_DATE(departure_time,'%h:%i%p') END
请注意,您指定了LIMIT子句,这可能会产生意外结果。
此外,如果您同时拥有AM和PM时间,则按时间顺序对AM / PM进行排序将不会按时间顺序排序。