如何计算每月的记录数?

时间:2019-03-29 05:07:58

标签: mysql

我想计算一年中每个月的记录数。

select count(t2.user) as total, concat(YEAR(t2.docdate), '-', MONTH(t2.docdate)) as date

from tbl1 t1
LEFT JOIN tbl2 t2 ON(...)

WHERE

AND t2.docdate BETWEEN '2018-10-01' AND '2019-03-25'

GROUP BY MONTH(t2.docdate)

total docdate

但是我的结果不正确。

18    2018-12-12
93    2018-12-12
51    2018-11-18
12    2018-11-07
13    2018-11-03

2 个答案:

答案 0 :(得分:3)

您需要按选择语句中包含的相同字段分组:

select count(t2.user) as total, concat(YEAR(t2.docdate), '-', MONTH(t2.docdate)) as date    
from tbl1 t1
LEFT JOIN tbl2 t2 ON(...)

WHERE

AND t2.docdate BETWEEN '2018-10-01' AND '2019-03-25'

GROUP BY concat(YEAR(t2.docdate), '-', MONTH(t2.docdate))

答案 1 :(得分:1)

使用dateformat尝试以下操作

$.ajax({
  type: 'POST',
  url: 'mysite/action',
  dataType: 'json',
  data: postData,
  success: function(response) {
    console.log(response);
    var input = $('<a class="btn btn-info" href="....response...." role="button">Link</a>');
    input.appendTo($("body"));
  }
});

在我看来,您需要使用table2作为左表而不是table1