MySQL SELECT COUNT具有多个字段

时间:2014-12-12 20:31:17

标签: mysql sql select group-by

我在MySQL表中有以下数据。

+---------------------+---------------------------+-----------------+
| AcctStartTime       | UserName                  |    ResponseCode |
+---------------------+---------------------------+-----------------+
| 2014-12-01 16:24:34 | 10000018@example.com |             408 |
| 2014-12-01 16:48:36 | 10000018@example.com |             200 |
| 2014-12-01 17:24:09 | 10000018@example.com |             408 |
| 2014-12-01 17:45:41 | 10000018@example.com |             200 |
| 2014-12-01 17:53:43 | 10000018@example.com |             408 |
| 2014-12-01 18:08:51 | 10000018@example.com |             200 |
| 2014-12-01 18:50:19 | 10000018@example.com |             200 |
| 2014-12-01 23:21:50 | 10000018@example.com |             502 |
| 2014-12-01 23:22:14 | 10000018@example.com |             487 |
| 2014-12-01 23:26:14 | 10000018@example.com |             484 |
| 2014-12-01 23:29:31 | 10000018@example.com |             408 |
| 2014-12-01 23:30:36 | 10000018@example.com |             487 |
| 2014-12-01 23:39:57 | 10000018@example.com |             487 |
| 2014-12-01 23:42:55 | 10000018@example.com |             200 |
| 2014-12-01 23:57:58 | 10000018@example.com |             200 |
| 2014-12-02 00:25:28 | 10000018@example.com |             200 |
| 2014-12-02 00:28:28 | 10000018@example.com |             200 |
| 2014-12-02 00:47:36 | 10000018@example.com |             200 |
| 2014-12-02 01:32:07 | 10000018@example.com |             200 |
| 2014-12-02 01:36:49 | 10000018@example.com |             200 |
| 2014-12-02 01:55:15 | 10000018@example.com |             200 |
| 2014-12-02 01:56:43 | 10000018@example.com |             200 |
| 2014-12-02 01:58:16 | 10000018@example.com |             487 |
| 2014-12-02 02:33:09 | 10000018@example.com |             484 |
| 2014-12-02 02:37:30 | 10000018@example.com |             408 |
| 2014-12-02 02:40:53 | 10000018@example.com |             487 |
| 2014-12-02 02:42:07 | 10000018@example.com |             484 |
| 2014-12-02 02:44:31 | 10000018@example.com |             200 |
| 2014-12-02 02:49:35 | 10000018@example.com |             408 |
| 2014-12-02 02:50:36 | 10000018@example.com |             408 |
| 2014-12-02 02:51:52 | 10000018@example.com |             200 |
| 2014-12-02 03:18:18 | 10000018@example.com |             487 |
| 2014-12-02 03:38:54 | 10000018@example.com |             200 |
| 2014-12-02 03:42:00 | 10000018@example.com |             200 |
| 2014-12-02 03:49:13 | 10000018@example.com |             200 |
| 2014-12-02 04:00:41 | 10000018@example.com |             487 |
| 2014-12-02 04:03:18 | 10000018@example.com |             487 |

我想知道每天我们得到的ResponsCode数量是多少,就像下面一样,我不知道如何在这里使用GROUP条件,我厌倦了很多选择但是我没有得到理想的输出。

+---------------------+---------------+-----------+----------+
|      date           |       200     |    503    |    487   |
+---------------------+---------------+-----------+-----------
| 2014-12-01          |         5     |      3    |      7   |     
| 2014-12-02          |         8     |      1    |      2   |     
| 2014-12-03          |         4     |      0    |      9   |   

2 个答案:

答案 0 :(得分:1)

您需要为此创建一个触控广告素材,每个SUM CASE ResponseCode {/ 1}}:

SELECT DATE(AcctStartTime) AS `date`
    ,SUM(CASE ResponseCode
        WHEN 200 THEN 1
        ELSE 0
    END) AS `200`
    ,SUM(CASE ResponseCode
        WHEN 503 THEN 1
        ELSE 0
    END) AS `503`
    ,SUM(CASE ResponseCode
        WHEN 407 THEN 1
        ELSE 0
    END) AS `407`
FROM WhateverYourTableIs
GROUP BY `date`
;

这应该可以为您提供所需的计数。

答案 1 :(得分:1)

更好,更简洁的方法就是这样

SELECT 
    DATE(AcctStartTime) AS `date`,
    SUM(ResponseCode = 200) AS `200`,
    SUM(ResponseCode = 503) AS `503`,
    SUM(ResponseCode = 407) AS `407`
FROM your_table
GROUP BY `date`;

这使用布尔结果,当为true时,它返回1将被求和,而当为

时为0