Curtis Crewe
Curtis Crewe

Reputation: 4336

Get mySQL rows from today using the hour

Is it possible to SELECT all rows from just today using DATE('today') and then grab rows based on a specified hour?

I'm assuming it'd be something like so? (I know it's not the correct syntax)

SELECT 
    *
FROM
    `table`
WHERE
    DATE(`datetime`) = DATE(`today`) 
AND
    HOUR(`datetime`) = '23'

Bare in the mind the datetime column is in datetime format

Upvotes: 0

Views: 23

Answers (1)

julienc
julienc

Reputation: 20355

You can use NOW() to get the current datetime, or CURDATE() to get the date only, and then the DATE() and HOUR() functions:

SELECT *
FROM my_table
WHERE DATE(my_datetime_field) = CURDATE() /* or DATE(NOW()) */
AND HOUR(my_datetime_field) = 23;

Upvotes: 4

Related Questions