使用/ PHP通过日期障碍进行MySQL SELECT

时间:2014-11-24 10:36:05

标签: php mysql

我有以下数据库,我想在特定时间内选择某些价格:

 id | slot_date  | slot_time | slot_price | hotel_id | room_type_id | status
 +--+------------+-----------+------------+----------+--------------+------+
 1  | 2014-10-29 | 21:00:00  | 5          | 5        | 6            | 0
 2  | 2014-10-29 | 22:00:00  | 10         | 5        | 6            | 0
 3  | 2014-10-29 | 23:00:00  | 12         | 5        | 6            | 0
 4  | 2014-10-30 | 00:00:00  | 22         | 5        | 6            | 0
 5  | 2014-10-30 | 01:00:00  | 22         | 5        | 6            | 0
 6  | 2014-10-30 | 02:00:00  | 22         | 5        | 6            | 0
 7  | 2014-10-30 | 03:00:00  | 22         | 5        | 6            | 0

我想抓住特定时间的价格而且我是这样做的:

SELECT * from rn_slots_prices WHERE hotel_id = '5' AND room_type_id = '6' AND slot_time >= '21:00:00' AND slot_time <= '23:00:00' AND slot_date = '2014-10-29' AND status = '0'

这给了我特定时间的价格。现在的问题是,如果选定的小时数越过日期障碍,那么从23:00到02:00。我尝试了以下(使用php解决方法检查结束和开始日期),但它不起作用?

SELECT * from rn_slots_prices WHERE hotel_id = '5' AND room_type_id = '6' AND (slot_time >= '23:00:00' AND slot_time <= '23:59:00' AND slot_date = '2014-10-29') OR (slot_time <= '02:00:00' AND slot_time >= '00:00:00' AND slot_date = '2014-10-30') AND status = '0' ORDER BY slot_time ASC

结果始终为零。 我可以找到PHP的方法(我认为),但必须有另一种方法吗?

1 个答案:

答案 0 :(得分:1)

将日期/时间转换为unix时间戳,以便比较:

SELECT * from rn_slots_prices WHERE hotel_id = '5' AND room_type_id = '6' AND status = '0'
AND UNIX_TIMESTAMP(CONCAT(slot_date,' ',slot_time)) BETWEEN
UNIX_TIMESTAMP('2014-10-29 23:00:00') AND UNIX_TIMESTAMP('2014-10-30 02:00:00')
ORDER BY slot_time ASC