145

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?

Foreever
  • 7,099
  • 8
  • 53
  • 55
Clinton J
  • 1,975
  • 3
  • 19
  • 31

7 Answers7

238

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';
Daniele Vrut
  • 2,835
  • 2
  • 22
  • 32
John Woo
  • 258,903
  • 69
  • 498
  • 492
  • 4
    what happens if its timestamp? – KD.S.T. Jul 19 '18 at 06:16
  • It bears noting that MySQL seems a bit picky about the date format; while either 2019/02/08 21:04:07 or 2019-02-08 21:04:07 produces the expected outcome, 02-08-2019 21:04:07, using the US date format, casts a much wider net. – David A. Gray Feb 09 '19 at 20:08
26

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.

Kneel-Before-ZOD
  • 4,141
  • 1
  • 24
  • 26
12

Try this.

SELECT * FROM la_schedule WHERE `start_date` > '2012-11-18';
Sami
  • 8,168
  • 9
  • 66
  • 99
Faizan Khattak
  • 862
  • 8
  • 22
4

I have tried but above not working after research found below the solution.

SELECT * FROM my_table where DATE(start_date) > '2011-01-01';

Ref

Suresh Kerai
  • 891
  • 1
  • 6
  • 20
4

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.

PS: As @NikolajHansen has pointed out in the comment, this might not be the ideal solution, since this could run the date function in every row.

dilantha111
  • 1,388
  • 1
  • 17
  • 19
  • 1
    you would then start out by running the date function on all rows in table la_schedule. then comparing it to the right side of the comparison - if its a large dataset - this could give you trouble – Nikolaj Hansen Jul 27 '23 at 09:36
  • @NikolajHansen. when thinking back again. Yes seems like it might have an optimization issue. I agree. I'll just amend the concern to the answer. Thanks – dilantha111 Jul 29 '23 at 07:10
2

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';
beatusfk
  • 451
  • 6
  • 4
0

If you are comparing timestamp - you could try following

select * from table where columnInTimestamp > ((UNIX_TIMESTAMP() * 1000) - (1*24*60*60*1000))

Here UNIX_TIMESTAMP()gives current timestamp where as "12460601000" is the timestamp for 1 day -- With this you can find data just got created in 1 day or 2 days etc.

Pravin Bansal
  • 4,315
  • 1
  • 28
  • 19