MySQL query between two date fields

I am trying to create a MySQL query that will take the current date and compare it with the two date fields that I have in my table and return rows matching this query.

here are my columns 1- [from_date], whose type is date 2- [to_date], which is also of type Date

the query should return strings between these dates.

here is my request

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

My problem is that it does not return anything. Should I switch my column type to DATETIME?

Thanks in advance.

+4
source share
5 answers

See the documentation here.
Use BETWEEN
Mysql documentation here

+2
source

You can use Now() to get the current date of the system.

+4
source

use between

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

Try to do NOW () on DATE ()

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

Read more about NOW () here

0
source

The DATE() function actually actually extracts a portion of a date from an expression of date or date and time. However, it does not return the current date as you plan.

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

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

If you are trying to find something between two values, MySQL also has a BETWEEN statement

0
source

Source: https://habr.com/ru/post/1481387/


All Articles