Note that there are some explanatory texts on larger screens.

plurals
  1. POmysql select records in last hour, without date
    primarykey
    data
    text
    <p>I have a table of daily emails to be sent. So I'm storing a time of day, as a time field (00:00:00, without a date). A couple example rows might be: </p> <pre><code>mike, timeOfDay = 07:03 meaning "send mike an email daily at 7:03AM" corey, timeOfDay = 23:30 meaning "send corey an email daily at 11:30PM" </code></pre> <p>At any given time, I want to find the emails to send from the last hour. For example, if I run this at 7:55AM, it should get all records between 6:55AM and 7:55AM. That will include mike's email. If the script runs at 12:15AM (00:15), it should get all records between 11:15PM (23:15) and 12:15AM (00:15). This will include corey's email.</p> <p>I've looked at <a href="https://stackoverflow.com/questions/6276110/mysql-select-records-1-hour-or-younger-on-datetime-column">every</a> <a href="https://stackoverflow.com/questions/2999416/find-records-produced-in-the-last-hour">example</a> on <a href="https://stackoverflow.com/questions/2999416/find-records-produced-in-the-last-hour">SO</a> and the web, and they all seem to require a date in addition to the hour. </p> <p>As close as I could come:</p> <pre><code>SELECT `notification`. *, now() as now, DATE_SUB( NOW( ) , INTERVAL 1 HOUR ) as 1hourAgo FROM `notification` WHERE ( `notification`.`timeofday` &gt; DATE_SUB( NOW( ) , INTERVAL 1 HOUR ) ) AND ( `notification`.`timeofday` &lt; NOW( ) ) </code></pre> <p>I figured out a hack to match the hours of this hour and last, before now, but it seems, well, hackish. There must be a correct way to do this!</p> <p>Here's the hack I've got.</p> <pre><code>SELECT `notification`. * , HOUR( NOW( ) ) AS HOUR , HOUR( DATE_SUB( NOW( ) , INTERVAL 1 HOUR ) ) AS 1hourago, time( now( ) ) AS now FROM `notification` WHERE ( HOUR( `notification`.`timeofday` ) = HOUR( NOW( ) ) OR HOUR( `notification`.`timeofday` ) = HOUR( DATE_SUB( NOW( ) , INTERVAL 1 HOUR ) ) ) AND ( `notification`.`timeofday` &lt; TIME( NOW( ) ) ) </code></pre> <p>Thanks for your help!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload