MySQL select all rows from last month until (now() - 1 month), for comparative purposes

后端 未结 10 493
北海茫月
北海茫月 2020-12-07 18:12

I need some help writing a MySQL query to show me rows from last month, but not the whole month, only up and until the same day, hour and minute as it is now(), but 1 month

10条回答
  •  借酒劲吻你
    2020-12-07 18:55

    My solution was to avoid using NOW() when writing sql with your programming language, and substitute with a string. The problem with NOW() as you indicate is it includes current time. So to capture from the beginning of the query day (0 hour and minute) instead of:

    r.date <= DATE_SUB(NOW(), INTERVAL 99 DAY)
    

    I did (php):

    $current_sql_date = date('Y-m-d 00:00:00');
    

    in the sql:

    $sql_x = "r.date <= DATE_SUB('$current_sql_date', INTERVAL 99 DAY)"
    

    With that, you will be retrieving data from midnight of the given day

提交回复
热议问题