简体   繁体   English

如果添加日期和当前日期之间的持续时间不大于或等于 2 小时,则获取数据

[英]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.如果用户的预订时间和当前持续时间在 2 小时之间,我想允许用户取消他们的订单,因为我想限制用户在他们的预订时间和当前持续时间大于或等于 2 小时时取消他们的预订。

I want to generate a query that returns all the bookings whose booking time is less than 2 hours.我想生成一个查询,返回所有预订时间少于 2 小时的预订。 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.然后使用此查询获得不到 2 小时的时间。

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.假设booking_time是MySQL 标准格式。

Try this and the below query will use index if you have one in booking_time column试试这个,如果你在booking_time列中有一个,下面的查询将使用索引

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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