简体   繁体   English

编辑现有查询以搜索时间是否在开始日期和结束日期之间

[英]Edit on existing query to search if time is between start date and end date

Through a form the user selects a date + time and then when he submits it, the code run this query 用户通过表单选择日期和时间,然后在提交日期时,代码运行此查询

ABS(TIMESTAMPDIFF( HOUR , `start`, :time )) < 2

that finds all the rows where the difference between start and selected date is less than 2. 查找start日期和所选日期之间的差小于2的所有行。

However, I now added a new field end that holds the end date+time of the event. 但是,我现在添加了一个新的字段end ,其中包含事件的结束日期和时间。

My question is how should I edit my existing query to return all the rows that the selected date is between start and end ? 我的问题是我应该如何编辑现有查询以返回所选日期在startend之间的所有行?

This is the format of selected , start and end date+time: 2012-07-25 04:30 这是selected的格式, 开始结束日期+时间: 2012-07-25 04:30

Use BETWEEN BETWEEN使用

WHERE selected_date BETWEEN start AND end

To quote the manual about using BETWEEN with dates: 引用有关在日期中使用BETWEEN的手册:

For best results when using BETWEEN with date or time values, use CAST() to explicitly convert the values to the desired data type. 为了在将BETWEEN与日期或时间值一起使用时获得最佳结果,请使用CAST()将这些值显式转换为所需的数据类型。 Examples: If you compare a DATETIME to two DATE values, convert the DATE values to DATETIME values. 示例:如果将一个DATETIME与两个DATE值进行比较,则将DATE值转换为DATETIME值。 If you use a string constant such as '2001-1-1' in a comparison to a DATE, cast the string to a DATE. 如果在与DATE的比较中使用字符串常量(例如“ 2001-1-1”),请将字符串转换为DATE。

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

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