How to use greater than operator with date?
No idea what is going on here. Here is the query, right from phpMyAdmin:
SELECT * FROM `la_schedule` WHERE 'start_date' >'2012-11-18';
But I consistently get all records in the table returned, including those with start date 2012-11-01. What gives?
you have enlosed start_date
with single quote causing it to become string, use backtick
instead
SELECT * FROM `la_schedule` WHERE `start_date` > '2012-11-18';
- SQLFiddle Demo
In your statement, you are comparing a string called start_date with the time.
If start_date is a column, it should either be
SELECT * FROM `la_schedule` WHERE start_date >'2012-11-18';
(no apostrophe) or
SELECT * FROM `la_schedule` WHERE `start_date` >'2012-11-18';
(with backticks).
Hope this helps.
Try this.
SELECT * FROM la_schedule WHERE `start_date` > '2012-11-18';
Adding this since this was not mentioned.
SELECT * FROM `la_schedule` WHERE date(start_date) > date('2012-11-18');
Because that's what actually works for me. Adding date() function on both comparison values.
In my case my column was a datetime it kept giving me all records. What I did is to include time, see below example
SELECT * FROM my_table where start_date > '2011-01-01 01:01:01';