简体   繁体   中英

MySQL query between 2 dates fields

i am trying to create a MySQL query that will take current date and compare it to the 2 date fields that i have in my table and return the rows that satisfy that query.

here is my columns 1- [from_date] which type is Date 2- [to_date] which also holds type Date

the query suppose to return the rows that falls in between those dates.

here is my query

mysql_query("SELECT * FROM location WHERE from_date >= DATE() AND to_date <= DATE()")

my problem is that it doesn't return anything. should i switch my column type to DATETIME?

Thanks in advance.

您应该使用Now()来获取系统的当前日期。

See documentation here.
Use BETWEEN
mysql documentation here

之间使用

mysql_query("SELECT * FROM location WHERE CURDATE() between from_date and to_date 

Try NOW() insted of DATE()

    mysql_query("SELECT * FROM location WHERE from_date >= NOW() AND to_date <= NOW()")

Get more about NOW() here

The DATE() function actually just extracts the date part from a date or datetime expression. It doesn't, however, not return the current date as you intend.

To get the current time use NOW() or CURDATE() :

mysql_query("SELECT * FROM location WHERE CURDATE() BETWEEN from_date AND to_date")

If trying to find a something between two values, MySQL also has the BETWEEN operator

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