Get the values for last 6 months in mysql

前端 未结 4 1064
梦谈多话
梦谈多话 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 11:06

     SELECT
    YEAR(DATE) AS YEAR,
    SUM(MONTH(DATE) = 1) AS JAN ,
    SUM(MONTH(DATE) = 2) AS FEB ,
    SUM(MONTH(DATE) = 3) AS MAR ,
    SUM(MONTH(DATE) = 4) AS APR,
    SUM(MONTH(DATE) = 5) AS MAY ,
    SUM(MONTH(DATE) = 6) AS JUNE ,
    SUM(MONTH(DATE) = 7) AS JULY ,
    SUM(MONTH(DATE) = 8) AS AUG,
    SUM(MONTH(DATE) = 9) AS SEP ,
    SUM(MONTH(DATE) = 10) AS OCTOBER ,
    SUM(MONTH(DATE) = 11) AS NOV ,
    SUM(MONTH(DATE) = 12) AS DECE
    FROM ratepersqft
    WHERE  DATE >= NOW() - INTERVAL 6 MONTH 
    GROUP BY 1;
    

提交回复
热议问题