简体   繁体   中英

Fetch a data if the time duration between added date and current date is not greater than or equal to 2 hours

I am creating a booking management system. I got stuck in the booking cancellation part. I want to allow users to cancel their orders if their booking time and the current time duration is between 2 hours because I want to restrict the users to cancel their booking if their booking time and current time duration is greater than or equal to 2 hours.

I want to generate a query that returns all the bookings whose booking time is less than 2 hours. How can I achieve this?

This is my database structure. 图片

You can extract the hour part of your date. Refer to this link

Then using this query to get those less than 2 hours.

SELECT * FROM table1 WHERE tdate - 120 < EXTRACT(MINUTE FROM now()) 
SELECT * FROM `TableName` where TIMEDIFF(NOW(),Your_date_ColumnName) < '02:00:00.000000'

Assuming that booking_time is in MySQL standard format.

Try this and the below query will use index if you have one in booking_time column

SELECT * 
FROM booking_table 
WHERE booking_time BETWEEN CURRENT_TIMESTAMP() - INTERVAL 2 HOUR AND CURRENT_TIMESTAMP()

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