Mysql计数和按日期分组

时间:2015-09-23 05:50:19

标签: php mysql

我在mysql中有这样的查询

SELECT COUNT(c_Id), DATE_FORMAT(c_Date,'%Y-%m') FROM tbl_complaintsheet  GROUP BY DATE_FORMAT(c_Date, '%Y%m')

现在结果是

COUNT(c_Id)  DATE_FORMAT(c_Date,'%Y-%m')  
-----------  -----------------------------
          2  2013-12                      
         48  2014-01                      
         85  2014-02                      
         95  2014-03                      
         93  2014-04                      
         63  2014-05                      
         94  2014-06                      
         57  2014-07                      
         70  2014-08                      
         87  2014-09                      
         83  2014-10                      
        101  2014-11                      
        117  2014-12                      
         86  2015-01                      
        126  2015-02                      
        100  2015-03                      
         92  2015-04                      
         82  2015-05                      
          1  2015-08                      
          8  2015-09                      

如何将参数传递给它,例如c_date = 2014?

3 个答案:

答案 0 :(得分:2)

尝试提取年份 -

FROM tbl_complaintsheet WHERE YEAR(c_date)=2014

答案 1 :(得分:2)

您可以尝试使用别名并使用类似

SELECT COUNT(c_Id), DATE_FORMAT(c_Date,'%Y-%m') AS dt FROM tbl_complaintsheet  WHERE dt LIKE '2014%' GROUP BY DATE_FORMAT(c_Date, '%Y%m')

答案 2 :(得分:0)

试试这个

SELECT COUNT(cd_Id), DATE_FORMAT(cd_Date,'%Y-%m') AS cd FROM  tbl_complaintsheet WHERE YEAR(cd_date) = 2014;