0 votes
1 view
in SQL by (22.4k 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.3k 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
0 votes
1 answer
0 votes
1 answer
asked Jul 9, 2019 in SQL by Tech4ever (22.4k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...