Robin Rodricks
Robin Rodricks

Reputation: 114046

MySQL - SELECT WHERE date < X

How do I do this in MySQL?

 SELECT * FROM MyTable WHERE MyDateCol < TODAY()

I googled a lot on the subject and didn't really find anything except "look in the MySQL date ref" which does not explain the above easily enough.

I know I can do the following which is kinda indirect:

SELECT * FROM MyTable WHERE MyDateCol BETWEEN (0000-00-00) AND TODAY()

Upvotes: 5

Views: 5203

Answers (4)

Paul Tomblin
Paul Tomblin

Reputation: 182802

If MySQL supports SYSDATE, you should use that instead of NOW() or CURDATE(), since it's more standard.

SELECT * FROM MyTable WHERE MyDateCol < SYSDATE;

Upvotes: 2

Ivan Nevostruev
Ivan Nevostruev

Reputation: 28733

Use NOW() function:

SELECT * FROM MyTable WHERE MyDateCol < NOW()

Upvotes: 1

Quassnoi
Quassnoi

Reputation: 425623

SELECT * FROM MyTable WHERE MyDateCol < CURDATE()

Upvotes: 2

Pascal MARTIN
Pascal MARTIN

Reputation: 401142

In MySQL, you have the curdate() funciton, that will get you the date of today.

So, something like this should do :

select *
from your_table
where your_date_column < curdate()


Quoting the manual's page of that function :

Returns the current date as a value in 'YYYY-MM-DD' or YYYYMMDD format, depending on whether the function is used in a string or numeric context.


And if you want to compare to the current time (and not only day), you can use the now() function :

Returns the current date and time as a value in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS.uuuuuu format

Upvotes: 4

Related Questions