Mysql – How to choose a row with the current date time?

I tried to select only today’s records from the database table.

Currently I use

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

But it needs the results of the past 24 hours, I need it to select only today’s results, ignore Time. How to select results based on date only?

Use DATE and CURDATE()

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

I want to use DATE and still use INDEX.

see the execution plan on the DEMO< /p>

I tried to select only today’s records from the database table.

Currently I use

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

But it needs the results of the past 24 hours, I need it to select only today’s Results, ignore time. How to select results based on date only?

Use DATE and CURDATE()

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

I want to use DATE and still use INDEX.

see the execution plan on the DEMO

Leave a Comment

Your email address will not be published.