简体   繁体   中英

Retrieve database records by comparing time field to current time and current time plus 1 hour

I have a database table used to store bookings which uses a time field(bookingTime) and a date field(bookingDate). I am trying to view records for the next hour by comparing the time field(bookingTime) to CURTIME. I get some results returned but not everything. I have tried to use NOW() and interval but nothing seems to return the correct results. The results do not show me a full hour of bookings and don't seem to go over the hour mark meaning it will show me a booking at 13:59 but not 14:01.

Here is the query I'm currently using:

SELECT
    TIME_FORMAT(bookingTime, '%H:%i') as bookingTime, 
DATE_FORMAT(bookingDate, '%d/%m/%Y') as bookingDate, 
jobId, callSign, name, pickupAdd, phoneNum, destAdd, jobType, carType 
FROM job 
WHERE TIME(bookingTime) between curtime(bookingTime) and curtime(bookingTime)+3600
order by DATE(bookingDate), TIME(bookingTime) asc

Any help is appreciated, thanks.

Your WHERE statement is full of errors. CURTIME() only returns the current time and does not take any parameters. You want to use TIME() to get the time portion of a timestamp. You also need to use INTERVAL to add time to a timestamp. You also need to compare the booking time to now/now+1 hour.

Try this:

WHERE TIME(bookingTime) between CURTIME() and CURTIME()+INTERVAL 1 HOUR
  AND DATE(bookingDate) = CURDATE()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM