Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MYSQL select where date within this day

Tags:

date

mysql

MY query looks like this:

SELECT COUNT(entryID) 
FROM table 
WHERE date >= DATE_SUB(CURDATE(), INTERVAL 1 DAY)

Will this count the rows whose date values are within the day (starting at 12:00; not within 24 hours)? If not, how do I do so?

like image 719
Hope4You Avatar asked Mar 30 '12 19:03

Hope4You


Video Answer


2 Answers

The following should be enough to get records within the current day:

SELECT COUNT(entryID) 
FROM table 
WHERE date >= CURDATE()

As Michael notes in the comments, it looks at all records within the last two days in its current form.

The >= operator is only necessary if date is actually a datetime - if it's just a date type, = should suffice.

like image 69
Paul Bellora Avatar answered Oct 26 '22 02:10

Paul Bellora


Here's the solution:

SELECT COUNT(entryID) FROM table WHERE DATE(date) >= CURDATE()

Since my date column is type DATETIME, I use DATE(date) to just get the date part, not the time part.

like image 27
Hope4You Avatar answered Oct 26 '22 02:10

Hope4You