-3

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. Image

3 Answers3

0

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()) 
0
SELECT * FROM `TableName` where TIMEDIFF(NOW(),Your_date_ColumnName) < '02:00:00.000000'
Fahmi
  • 37,315
  • 5
  • 22
  • 31
hitaxi
  • 27
  • 5
0

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()
James
  • 1,819
  • 2
  • 8
  • 21
  • I am generating this query `SELECT * FROM booking WHERE added_date BETWEEN CURRENT_TIMESTAMP() - INTERVAL 2 HOUR AND CURRENT_TIMESTAMP() AND sender_id = "4" ORDER BY booking.id DESC` but it soes not return any row. – Alisha Lamichhane Aug 05 '19 at 06:26
  • Does `sender_id =4` is in the 2 hours time? Just remove `AND sender_id="4" ORDER BY booking.id DESC` then check what sender_id are really present – James Aug 05 '19 at 06:28
  • It does not return any row even after removing sender_id. And,the sender_id is int data, that is the user id. – Alisha Lamichhane Aug 05 '19 at 06:31
  • Then you don't have any booking who created 2 hour before? – James Aug 05 '19 at 06:34
  • I don't want bookings of two hours before. I want bookings that haven't crossed two hours from the current time. Suppose, if it is 2.00 pm, I want data that are booked between 12.00pm - 2.00pm. – Alisha Lamichhane Aug 05 '19 at 07:52
  • That's what i have shared you. `CURRENT_TIMESTAMP()` which gives you 2.00 pm `CURRENT_TIMESTAMP() - INTERVAL 2 HOUR` which gives 12.00 pm so on comparison it will show the data which is between those time for `added_date`. If its not showing share the table struc and sample data – James Aug 05 '19 at 08:02
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/197479/discussion-between-alisha-lamichhane-and-james). – Alisha Lamichhane Aug 05 '19 at 08:06
  • @AlishaLamichhane I hope your timezone is different in your application rather than in mysql – James Aug 05 '19 at 11:05