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

后端 未结 10 461
北海茫月
北海茫月 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 19:03

    Simple code please check

    SELECT * FROM table_name WHERE created <= (NOW() - INTERVAL 1 MONTH)
    
    0 讨论(0)
  • 2020-12-07 19:04
    SELECT * 
    FROM table 
    WHERE date BETWEEN 
        ADDDATE(LAST_DAY(DATE_SUB(NOW(),INTERVAL 2 MONTH)), INTERVAL 1 DAY) 
        AND DATE_SUB(NOW(),INTERVAL 1 MONTH);
    

    See the docs for info on DATE_SUB, ADDDATE, LAST_DAY and other useful datetime functions.

    0 讨论(0)
  • 2020-12-07 19:12

    This is an example of a MySQL date operation relevant to your question:

    SELECT DATE_ADD( now( ) , INTERVAL -1 MONTH ) 
    

    The above will return date time one month ago

    So, you can use it, as follows:

    SELECT * 
    FROM your_table 
    WHERE Your_Date_Column BETWEEN '2011-01-04' 
        AND DATE_ADD(NOW( ), INTERVAL -1 MONTH )
    
    0 讨论(0)
  • 2020-12-07 19:12

    maybe like this - all last month DATE(recDate) BETWEEN DATE_ADD(LAST_DAY(DATE_SUB(CURDATE(), INTERVAL 2 MONTH)), INTERVAL 1 DAY) AND LAST_DAY(DATE_SUB(CURDATE(), INTERVAL 1 MONTH) )

    0 讨论(0)
提交回复
热议问题