简体   繁体   English

当count为0时,MySQL每天计数

[英]MySQL Counts per day when count is 0

I have a MySQL query: 我有一个MySQL查询:

SELECT COUNT(*) AS total,  DATE_FORMAT(event.serverTime,'%Y-%m-%d') AS sdate
                    FROM event
                    WHERE
                    event.serverTime >= :startDate
                    AND event.serverTime <= :endDate 
                    GROUP BY sdate;

Which correctly returns something like: 哪个正确返回如下内容:

2011-08-10 => 5  
2011-08-15 => 6

However, I would like to also get the dates where there was 0 counts. 但是,我想得到0计数的日期。 So assuming startDate is 2011-08-10 and endDate is 2011-08-15, I would see: 所以假设startDate是2011-08-10而endDate是2011-08-15,我会看到:

2011-08-10 => 5  
2011-08-11 => 0  
2011-08-12 => 0  
2011-08-13 => 0  
2011-08-14 => 0  
2011-08-15 => 6

I am using PHP so in theory I could do some complex looping and fill up the gaps somehow, but I am wondering if there is a better solution? 我正在使用PHP,所以理论上我可以做一些复杂的循环并以某种方式填补空白,但我想知道是否有更好的解决方案?

Note that if no good MySQL solution exist, I'm also open to good PHP solutions 请注意,如果没有好的MySQL解决方案,我也会接受良好的PHP解决方案

If you want count items per day with 0 result, 如果你想要每天计数项目0结果,

try this : 尝试这个 :

set @date_start := (SELECT MIN(date_col) FROM my_table), 
    @date_end := (SELECT MAX(date_col) FROM my_table), 
    @i := 0;
SELECT DATE(ADDDATE(@date_start, INTERVAL @i:=@i+1 DAY)) AS date,
IFNULL((
    SELECT COUNT(*) FROM my_table AS m2
    WHERE DATE(m2.date_col) = DATE(ADDDATE(@date_start, INTERVAL @i DAY))
),0) AS total
FROM my_table AS m1
HAVING @i < DATEDIFF(@date_end, @date_start)

output : 输出:

[
    {
        "date": "2017-03-01",
        "total": "0"
    },
    {
        "date": "2017-03-02",
        "total": "0"
    },
    {
        "date": "2017-03-03",
        "total": "0"
    },
    {
        "date": "2017-03-04",
        "total": "0"
    },
    {
        "date": "2017-03-05",
        "total": "0"
    }
]

I think a possible solution you can look for is to create a table populated with all the dates and then have a join with the table.The table wont be very big as it contains just 365 rows for one year.Prepopulate this table and join with this table for your query. 我认为你可以寻找的一个可能的解决方案是创建一个填充了所有日期的表,然后与表连接。表不会很大,因为它只包含365行一年。预计此表并加入此表供您查询。

The benefit here is you dont have do any complex looping of dates inside your php every time this query get called. 这里的好处是,每次调用此查询时,您都不会在php中执行任何复杂的日期循环。 you populated your table once and use it over and over. 你填充了一次你的桌子并一遍又一遍地使用它。

You need to make a table with dates and join against that. 您需要创建一个包含日期的表并加入其中。

table temp_dates
id integer auto_increment PK
mydate date  <<-- consecutive dates

Now do the following query 现在执行以下查询

SELECT count(e.servertime) as total
       , td.mydate as sdate
FROM event e
RIGHT JOIN temp_dates td ON (td.mydate = date(e.servertime))
WHERE td.mydate BETWEEN :startdate AND :enddate
GROUP BY td.mydate 
HAVING total = 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM