Mysql 获取字段的和

Mysql 获取字段的和,mysql,sql,datetime-generation,Mysql,Sql,Datetime Generation,可能重复: 我有一个问题,我需要显示字段的总和。假设我有一个记录集,如下所示 detectDate |isp |infection | count -------------------------------------- 2012-10-02 01:00|aaaa |malware |3 2012-10-02 01:30|bbbb |malware |2 2012-10-02 01:33|bbbb |spy-eye |2 2012-10-02

可能重复:

我有一个问题,我需要显示字段的总和。假设我有一个记录集,如下所示

detectDate      |isp    |infection  | count
--------------------------------------
2012-10-02 01:00|aaaa   |malware    |3
2012-10-02 01:30|bbbb   |malware    |2
2012-10-02 01:33|bbbb   |spy-eye    |2
2012-10-02 01:45|aaaa   |DDos       |1
2012-10-03 01:50|cccc   |malware    |2
2012-10-03 02:00|dddd   |TDSS       |2
2012-10-03 04:50|dddd   |TDSS       |3
detectDate  |infection  | count
-------------------------------
2012-10-02  |DDos       |1
2012-10-02  |malware    |5
2012-10-02  |spy-eye    |2
2012-10-02  |TDSS       |0
2012-10-03  |DDos       |0
2012-10-03  |malware    |2
2012-10-03  |spy-eye    |0
2012-10-03  |TDSS       |5
我想显示一个输出,它将显示每天所有感染的总和,如下所示

detectDate      |isp    |infection  | count
--------------------------------------
2012-10-02 01:00|aaaa   |malware    |3
2012-10-02 01:30|bbbb   |malware    |2
2012-10-02 01:33|bbbb   |spy-eye    |2
2012-10-02 01:45|aaaa   |DDos       |1
2012-10-03 01:50|cccc   |malware    |2
2012-10-03 02:00|dddd   |TDSS       |2
2012-10-03 04:50|dddd   |TDSS       |3
detectDate  |infection  | count
-------------------------------
2012-10-02  |DDos       |1
2012-10-02  |malware    |5
2012-10-02  |spy-eye    |2
2012-10-02  |TDSS       |0
2012-10-03  |DDos       |0
2012-10-03  |malware    |2
2012-10-03  |spy-eye    |0
2012-10-03  |TDSS       |5
我使用了这个查询

SELECT DATE_FORMAT( detectDate, '%Y-%m-%d' ) AS detectDate, infection, SUM( count )
FROM `tbl_correlateddata`
GROUP BY DATE_FORMAT( detectDate, '%Y-%m-%d' ) , infection
但是它只给出了一个输出,如下所示,这不是我的要求

detectDate  |infection  | count
-------------------------------
2012-10-02  |DDos       |1
2012-10-02  |malware    |5
2012-10-02  |spy-eye    |2
2012-10-03  |malware    |2
2012-10-03  |TDSS       |5
任何帮助都会非常有用:) 非常感谢:)非常感谢:)

编辑: 可能的副本:

但不相似

SELECT e.*, COALESCE(SUM(d.`count`),0) `SUM of count`
FROM
(
  SELECT c.detectDate, a.infection
  FROM
    (
      SELECT  DISTINCT infection
      FROM    tbl_correlateddata
    ) a CROSS JOIN
    (
      SELECT  DISTINCT DATE(detectDate) detectDate 
      FROM    tbl_correlateddata 
    ) c
) e LEFT JOIN tbl_correlateddata d
    ON  DATE(d.detectDate) = e.detectDate AND
        d.infection = e.infection
 GROUP BY detectDate, infection
 ORDER BY e.detectDate, e.infection


你的桌子上有所有可能的感染吗?一次感染可能发生在同一天,而同一次感染可能不会在第二天出现。这就是为什么。谢谢你的回答。是的。谢谢你指给我看那篇帖子。它有点一样:)只是它比你需要的更复杂,因为你不需要缺少填写的日期,仅缺少感染,是否?“选择…….总和(置信度)”。你从哪里得到信心专栏的???一如既往。。谢谢你的回答:)但是小编辑,我需要
计数总和
字段:)它的工作原理是:)我可以知道当你使用null时,如何为那些给出
null
的人获取0,或者我必须通过我的后端应用程序来完成吗???@Hasitha updated。添加了
COALESCE
。哦..非常感谢..你是个救生员..非常感谢:)@JohnWoo-i。e、 救生员+1。