在php月份计数中获取开始日期到结束日期之间的日期

时间:2016-06-02 10:43:23

标签: php mysql date

在php中如何按月计算每个用户的下列数据

SELECT * FROM sales_info WHERE assigned_dse_name IN ('ANBHAZHAGAN .') AND invoice_date BETWEEN '2016-01-01 00:00:00' AND '2016-06-01 23:59:59'

想要计数

 month   count
  01      0
  02      0
  03      5
  04      3
  05      7
  06      4

如何使用拆分月份循环来使用查询。

4 个答案:

答案 0 :(得分:0)

这样做:

SELECT
    MONTH(invoice_date) AS month,
    COUNT(*) as count 
FROM sales_info 
WHERE
    assigned_dse_name IN ('ANBHAZHAGAN .') AND
    invoice_date BETWEEN '2016-01-01 00:00:00' AND '2016-06-01 23:59:59'
GROUP BY MONTH(invoice_date)

答案 1 :(得分:0)

SELECT month,count(*)
FROM sales_info 
WHERE assigned_dse_name IN ('ANBHAZHAGAN') AND invoice_date BETWEEN '2016-01-01 00:00:00' AND '2016-06-01 23:59:59'
group by month

答案 2 :(得分:0)

请检查以下链接以获取答案

<强> Click here for Example
 SELECT MONTH(invoice_date)为Month_no,count(*)为Count
    来自sales_info     WHERE assigned_dse_name IN(&#39; ANBHAZHAGAN。&#39;)AND invoice_date BETWEEN&#39; 2016-01-01 00:00:00&#39; AND&#39; 2016-06-01 23:59:59&#39;按月分组(invoice_date)

答案 3 :(得分:0)

试试这个;)

SELECT `mouth`, sum(`count`) AS `count` 
FROM (
    SELECT LPAD(MONTH(invoice_date), 2, '0') AS `month`, COUNT(1) as `count`
    FROM sales_info
    WHERE assigned_dse_name IN ('ANBHAZHAGAN .') AND invoice_date BETWEEN '2016-01-01 00:00:00' AND '2016-06-01 23:59:59'
    GROUP BY MONTH(invoice_date)
    UNION
    SELECT LPAD(t.`month`, 2, '0') AS `month`, t.`count` FROM (
        SELECT 1 AS `month`, 0 AS `count` UNION
        SELECT 2 AS `month`, 0 AS `count` UNION
        SELECT 3 AS `month`, 0 AS `count` UNION
        SELECT 4 AS `month`, 0 AS `count` UNION
        SELECT 5 AS `month`, 0 AS `count` UNION
        SELECT 6 AS `month`, 0 AS `count` UNION
        SELECT 7 AS `month`, 0 AS `count` UNION
        SELECT 8 AS `month`, 0 AS `count` UNION
        SELECT 9 AS `month`, 0 AS `count` UNION
        SELECT 10 AS `month`, 0 AS `count` UNION
        SELECT 11 AS `month`, 0 AS `count` UNION
        SELECT 12 AS `month`, 0 AS `count`) t
    WHERE t.`month` >= MONTH('2016-01-01 00:00:00') AND t.`month` <= MONTH('2016-06-01 23:59:59'))TMP
GROUP BY `month`
ORDER BY `month`