按时间预订

时间:2016-11-02 19:22:12

标签: mysql

我遇到了MySQL的问题,我试图获得所有保留,其中starttime大于'16:42'但小于'18:42'。我试过这个,但是当我使用'15:42'而不是'16:42'时,我仍然得到相同的结果。

import falcon


class MyAPI:

    def on_get(self, req, resp):
        # You can access self.session here
        # self.session.add(foo)
        # self.session.commit()

This is the result of the query

2 个答案:

答案 0 :(得分:1)

可能是您有一些转换问题..尝试使用显式转换

SELECT     * 
FROM       `tables` 
INNER JOIN reservation_table 
ON         `tables`.`id` = reservation_table.table_id 
INNER JOIN reservations 
ON         reservations.id = reservation_table.reservation_id 
INNER JOIN customers 
ON         customers.id = reservations.customer_id 
WHERE  `date` = '2016-11-02' 
   AND ( starttime <= str_to_date('19:42', '%H:%i' )
         AND starttime >= str_to_date('16:42' , '%H:%i')) 

答案 1 :(得分:0)

如果starttimeendtime列的格式为TIME,则此查询可以解决您的问题:

因为正如mysql文档所说&#34;如果启用了NO_ZERO_DATE或NO_ZERO_IN_DATE SQL模式,则不允许零日期或部分日期。&#34; 您可能需要先执行此查询:

SET sql_mode = '';

然后是第二个(如果starttime和endtime列是TIME格式):

SELECT     *
FROM       `tables` 
INNER JOIN reservation_table 
ON         `tables`.`id` = reservation_table.table_id 
INNER JOIN reservations 
ON         reservations.id = reservation_table.reservation_id 
INNER JOIN customers 
ON         customers.id = reservations.customer_id 
WHERE  `date` = '2016-11-02'
AND ( starttime <= str_to_date('19:42', '%H:%i')
    AND starttime >= str_to_date('16:42' , '%H:%i')) 

但如果这些列不是那种格式,请执行:

SELECT     *
FROM       `tables` 
INNER JOIN reservation_table 
ON         `tables`.`id` = reservation_table.table_id 
INNER JOIN reservations 
ON         reservations.id = reservation_table.reservation_id 
INNER JOIN customers 
ON         customers.id = reservations.customer_id 
WHERE  `date` = '2016-11-02'
AND ( str_to_date(starttime, '%H:%i') <= str_to_date('19:42', '%H:%i')
    AND str_to_date(starttime, '%H:%i') >= str_to_date('16:42' , '%H:%i'))