简体   繁体   中英

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM