Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

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?

1 Answer

0 votes
by (40.7k points)

Try using DATE and CURDATE() like this:

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

I guess using DATE still uses INDEX.

Have a look at the execution plan on the DEMO:

http://www.sqlfiddle.com/#!2/72440/2

Related questions

0 votes
1 answer
0 votes
1 answer
asked Dec 29, 2020 in SQL by Appu (6.1k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...