How to select rows that have current day's timestamp?

Jackie Honson picture Jackie Honson · Feb 8, 2013 · Viewed 206.4k times · Source

I am trying to select only today's records from a database table.

Currently I use

SELECT * FROM `table` WHERE (`timestamp` > DATE_SUB(now(), INTERVAL 1 DAY));

But this takes results for the last 24 hours, and I need it to only select results from today, ignoring the time. How can I select results based on the date only ?

Answer

John Woo picture John Woo · Feb 8, 2013

use DATE and CURDATE()

SELECT * FROM `table` WHERE DATE(`timestamp`) = CURDATE()

I guess using DATE still uses INDEX.

see the execution plan on the DEMO