Count()不会返回零MySQL

时间:2015-03-23 18:35:53

标签: mysql

我是一名Web开发人员,并试图在mysql查询上搜索解决方案。我无法为count()函数获得正确的解决方案返回零。 count()函数不会为所有日期返回零。

查询如下。任何人都可以帮助我。

SELECT
    count(stat_id) as typeSuccess,
    device_type as typeName,
    YEARWEEK(date_auth) as date
FROM auth_stat
WHERE
    AUTH_RESULT = 'SUCCESS' AND
    date_auth BETWEEN DATE_SUB(CURDATE(), INTERVAL 6 WEEK) AND CURDATE()
GROUP BY YEARWEEK(date_auth), device_type
ORDER BY YEARWEEK(date_auth)

我试图形成的查询是

select
    date_auth,
    count(stat_id) as typeSuccess,
    device_type as typeName,
    YEARWEEK(date_auth) as date
from
( 
    select @curDate := Date_Add(@curDate, interval 1 day) as MyJoinDate
    from
    ( 
        select @curDate := CURDATE()
    ) sqlvars, auth_stat limit 18 
) dateAll
LEFT JOIN auth_stat U on dateAll.MyJoinDate = U.date_auth
group by dateAll.MyJoinDate

实际输出:

+------------+-------------+
| date       | typeSuccess |
+------------+-------------+
| 2015-03-18 |          11 |
+------------+-------------+

预期产出:

+------------+-------------+
| date       | typeSuccess |
+------------+-------------+
| 2015-03-18 |          11 |
| 2015-03-19 |          0  |
| 2015-03-20 |          0  |
+------------+-------------+

4 个答案:

答案 0 :(得分:2)

我相信你想做的就是这个

SELECT
    SUM(AUTH_RESULT='SUCCESS') as numSuccess,
    YEARWEEK(date_auth) as date
FROM auth_stat
WHERE date_auth BETWEEN DATE_SUB(CURDATE(), INTERVAL 6 WEEK) AND CURDATE()
GROUP BY YEARWEEK(date_auth), device_type
ORDER BY YEARWEEK(date_auth)

基本上我所做的就是告诉MySQL在auth_result成功时总结布尔值(0或1)。如果没有成功,SUM()将返回特定周的0在那一周

主要问题是你过滤掉所有不成功的auth_results,然后不计算。所以从那里删除它,你应该是好的!

如果你每天都需要,那么你可以这样做

SELECT
    SUM(AUTH_RESULT='SUCCESS') as numSuccess,
    DATE(date_auth) as date
FROM auth_stat
WHERE date_auth BETWEEN DATE_SUB(CURDATE(), INTERVAL 6 WEEK) AND CURDATE()
GROUP BY DATE(date_auth), device_type
ORDER BY DATE(date_auth)

SQLFIDDLE FOR BOTH RESULTS

答案 1 :(得分:0)

尝试

SELECT COUNT(statid), dates.auth_date 
FROM 
(   SELECT DISTINCT auth_date 
    FROM Table1 
    WHERE auth_date BETWEEN '2015-03-06' AND '2015-03-09' -- or whatever
) AS dates
LEFT JOIN Table1 ON TRUE
 AND Table1.auth_date = dates.auth_date
 AND table1.auth_result = 'SUCCESS'
GROUP BY dates.auth_date
ORDER BY dates.auth_date

如此处所述:http://sqlfiddle.com/#!9/84faf/2 例如数据:

+--------+-------------+------------+
| statid | auth_result | auth_date  |
+--------+-------------+------------+
|      1 | SUCCESS     | 2015-03-05 |
|      2 | SUCCESS     | 2015-03-05 |
|      3 | SUCCESS     | 2015-03-05 |
|      4 | OTHER       | 2015-03-06 |
|      5 | OTHER       | 2015-03-06 |
|      6 | OTHER       | 2015-03-06 |
|      7 | SUCCESS     | 2015-03-07 |
|      8 | SUCCESS     | 2015-03-07 |
|      9 | SUCCESS     | 2015-03-07 |
|     10 | OTHER       | 2015-03-08 |
|     11 | SUCCESS     | 2015-03-08 |
|     12 | OTHER       | 2015-03-08 |
|     13 | SUCCESS     | 2015-03-09 |
|     14 | SUCCESS     | 2015-03-09 |
|     15 | SUCCESS     | 2015-03-09 |
|     16 | OTHER       | 2015-03-10 |
|     17 | OTHER       | 2015-03-10 |
|     18 | SUCCESS     | 2015-03-10 |
|     19 | OTHER       | 2015-03-11 |
+--------+-------------+------------+

例如输出:

+---------------+-------------------------+
| count(statid) |        auth_date        |
+---------------+-------------------------+
|             0 | March, 06 2015 00:00:00 |
|             3 | March, 07 2015 00:00:00 |
|             1 | March, 08 2015 00:00:00 |
|             3 | March, 09 2015 00:00:00 |
+---------------+-------------------------+

答案 2 :(得分:0)

它并不美丽,但希望它有所帮助:

SELECT
    date_auth,
    count(stat_id) as typeSuccess,
    device_type as typeName,
    YEARWEEK(date_auth) as date
FROM
( 
    SELECT 
   DATE(DATE_ADD(NOW(), interval tmp.id day)) AS MyJoinDate
   FROM (SELECT 1 as id
         UNION ALL SELECT 2
         UNION ALL SELECT 3
         UNION ALL SELECT 4
         UNION ALL SELECT 5
         UNION ALL SELECT 6
         UNION ALL SELECT 7
         UNION ALL SELECT 8
         UNION ALL SELECT 9
         UNION ALL SELECT 10
         UNION ALL SELECT 11
         UNION ALL SELECT 12
         UNION ALL SELECT 13
         UNION ALL SELECT 14
         UNION ALL SELECT 15
         UNION ALL SELECT 16
         UNION ALL SELECT 17
         UNION ALL SELECT 18
   ) AS tmp 
) dateAll
LEFT JOIN auth_stat U on dateAll.MyJoinDate = U.date_auth
group by dateAll.MyJoinDate

答案 3 :(得分:0)

我尝试添加如下查询。它仍然会返回具有计数的记录,而不是具有计数为零的日期。

SELECT COUNT(stat_id), dates.date_auth 
FROM 
(   SELECT DISTINCT date_auth 
    FROM auth_stat 
    WHERE date_auth BETWEEN DATE_SUB(CURDATE(), INTERVAL 20 DAY) AND       CURDATE()
) AS dates
LEFT JOIN auth_stat ON TRUE
 AND auth_stat.date_auth = dates.date_auth
 AND auth_stat.auth_result = 'SUCCESS'
GROUP BY dates.date_auth
ORDER BY dates.date_auth

上述查询的输出结果:

COUNT(stat_id)date_auth升序1
1 2015-03-05 00:00:00 1 2015-03-06 00:00:00 11 2015-03-18 00:00:00