从mysql中的表中获取所有日期数据的计数总和

从mysql中的表中获取所有日期数据的计数总和,mysql,datetime,select,Mysql,Datetime,Select,现在,我需要一个查询来获取基于日期和类型的详细信息 比如,我想要日期的callType总计数的总和 Table: call_details ------------------- userId phoneNumber callDate callType callDuration 我的问题- 28-03-2014 - there are total 1 missed call, 2 incoming call and 5 outgoing calls 29-03-2014 - ther

现在,我需要一个查询来获取基于日期和类型的详细信息

比如,我想要日期的callType总计数的总和

Table: call_details
-------------------
userId

phoneNumber

callDate

callType

callDuration
我的问题-

28-03-2014 - there are total 1 missed call, 2 incoming call and 5 outgoing calls

29-03-2014 - there are total 0 missed call, 5 incoming call and 10 outgoing calls.

30-03-2012 - there are total 5 missed call, 10 incoming call and 35 outgoing calls.

结果是错误的,因为它没有给我单独的结果日期。它只是在数据库中选择日期并显示所有内容。我希望所有类型的结果按日期分开,且其计数为全天。

您可以使用条件聚合:

SELECT `callType`, `callDate`, count(`callType`) as type FROM `tbl_call_details` GROUP BY `callType` ORDER BY `callDate` ASC


callType    callDate    count

OUTGOING    2014-03-28  52
INCOMING    2014-03-30  11
MISSED  2014-03-31  1
这会将值放入列中。如果希望将它们作为字符串,则可以执行以下操作:

SELECT date(`callDate`),
       sum(calltype = 'MISSED') as NumMissed,
       sum(callType = 'INCOMING') as NumIncoming,
       sum(callType = 'OUTGOING') as NumOutgoing
FROM `tbl_call_details`
GROUP BY date(`callDate`)
ORDER BY date(`callDate`) ASC;

您还希望按callDate进行分组,如下所示:

SELECT date(`callDate`),
        concat('there are ', sum(calltype = 'MISSED'), ' missed call, '
               sum(callType = 'INCOMING'), ' incoming call and '
               sum(callType = 'OUTGOING'), ' outgoing calls'
              )
FROM `tbl_call_details`
GROUP BY date(`callDate`)
ORDER BY date(`callDate`) ASC;

第一个查询是获取所有记录并显示计数。我不想要每个记录,我想要问题中所示日期的总和。在此查询中,它显示所有类型的计数,而不是所有日期的计数总和。如果
calldate
也包含时间,则会发生这种情况。calldate也有时间。所以我们不能像我说的那样得到数据,如果我们把callDate作为时间戳?完成。我用date铸造了callDate,它成功了。非常感谢。在您的查询中,我也得到了62行,包括count。我不希望每个计数都像传入的-28-03-2014-1,传入的-28-03-2014-1一样。我想要具体日期的数字总和。类似于传入-28-03-2014-5,然后传入-29-03-2014-10。我想这样,但在这个查询中,它给了我一个一个的计数,而不是计数的总和。非常确定这个查询符合您的要求,我在其他数据库上测试了类似的东西,也许您没有粘贴所有的数据?选择
callType
callDate
,count(
callType
)作为类型,从
tbl\u call\u details
分组依据
callDate
callType
订单依据
callDate
ASC
SELECT `callType`, `callDate`, count(`callType`) as type FROM `tbl_call_details` GROUP BY `callDate`, `callType` ORDER BY `callDate` ASC