简体   繁体   中英

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.

However, I now added a new field end that holds the end date+time of the event.

My question is how should I edit my existing query to return all the rows that the selected date is between start and end ?

This is the format of selected , start and end date+time: 2012-07-25 04:30

Use BETWEEN

WHERE selected_date BETWEEN start AND end

To quote the manual about using BETWEEN with dates:

For best results when using BETWEEN with date or time values, use CAST() to explicitly convert the values to the desired data type. Examples: If you compare a DATETIME to two DATE values, convert the DATE values to DATETIME values. If you use a string constant such as '2001-1-1' in a comparison to a DATE, cast the string to a DATE.

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