在mysql中,如何使用条件按两列对表进行排序?
Mysql 第一个查询:在这个查询中,我按出发时间下订单。 但我要的是所有有空位0的记录,这些记录应该只在出发时间之前才有订单 所需输出在mysql中,如何使用条件按两列对表进行排序?,mysql,Mysql,Mysql 第一个查询:在这个查询中,我按出发时间下订单。 但我要的是所有有空位0的记录,这些记录应该只在出发时间之前才有订单 所需输出 是否可以使用mysql查询。或者通过其他方式???您可以首先使用where和order by语句对可用座位的非零值的出发时间进行排序,然后可以应用union和order by,以增加时间顺序追加零列可用座位。代码可以如下所示: select origin_id, destination_id,travel_date,departure_time,availab
是否可以使用mysql查询。或者通过其他方式???您可以首先使用where和order by语句对可用座位的非零值的出发时间进行排序,然后可以应用union和order by,以增加时间顺序追加零列可用座位。代码可以如下所示:
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;
简言之,在联盟第一部分的可用位置添加0个席位,
然后
联合后应用可用的\u seats=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时间进行排序将不会按时间顺序进行排序。No Krishna我们只能使用union/union all(即在查询结束时)进行一次排序。例如:query1 union query2 order by column asc;因为union将在一个表中返回结果,我们可以在该表上应用order by。是的。我得到了它。谢谢您如何建议解决方案?如果可用的座位数为零,则CASE表达式的计算结果为常量99:99,否则它的计算结果为您的sortkey。这将导致零座位行在末尾排序。请阅读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 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;
ORDER BY CASE
WHEN available_seats=0 THEN '99:99'
else STR_TO_DATE(departure_time,'%h:%i%p') END