Get the values for last 6 months in mysql

前端 未结 4 1061
梦谈多话
梦谈多话 2021-01-14 10:33

I need to get last six month values from the database. Here is my Fiddle. I need to get the values dynamically. Now February. So I need August to Januar

4条回答
  •  清酒与你
    2021-01-14 10:56

    For MYSQL: you may use date_add:

     SELECT * FROM ratepersqft 
    WHERE date < Now() and date > DATE_ADD(Now(), INTERVAL- 6 MONTH);
    

    For SQL Server:, dateadd :

      SELECT * FROM ratepersqft 
     WHERE date < Now() and date > DATEADD(Month, -6, Now());
    

    http://www.sqlfiddle.com/#!2/1f8029/48

    **Please wrap your date column name with backticks given it is a reserved key word. **

    MYSQL update:

    SQLFIDDLE DEMO

    SELECT *
    FROM ratepersqft 
    WHERE date_format(date,'%Y-%m') < 
                         date_format(now(),'%Y-%m')
    and date_format(date,'%Y-%m') >= 
                         date_format(now() - interval 6 month,'%Y-%m')
    order by date desc;
    

提交回复
热议问题