2

我有一个数据表如下,

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      |malware    |5
2012-10-02      |spy-eye    |2
2012-10-02      |DDos       |1
2012-10-03      |malware    |2
2012-10-03      |TDSS       |5

我知道如何使用组,但不是他的远:(你能帮我吗?我需要按日期和感染分组,并得到如图所示的计数字段的总数。

4

3 回答 3

3

使用SUM函数对列求和count,使用DATE_FORMAT函数仅按日期分组。

SELECT DATE_FORMAT(detectDate, '%Y-%m-%d') AS detectDate
      ,infection 
      ,SUM(`COUNT`) as `count`
FROM myTable
GROUP BY DATE_FORMAT(detectDate, '%Y-%m-%d'), infection

看到这个 SQLFiddle

于 2012-10-31T05:13:53.417 回答
1

您应该使用DATE mySql 函数从日期/时间列中提取日期部分:

select DATE(detectDate),infection, sum(count) 
from t group by DATE(detectDate),infection
order by  DATE(detectDate);
于 2012-10-31T05:18:28.913 回答
-1
select detectDate, infection, sum(count) as count
from table_name
group by detectDate, infection
于 2012-10-31T05:15:06.000 回答