编写查询时需要帮助

时间:2014-06-12 08:41:05

标签: php mysql sql arrays database

我有一张保存电话记录的表格。我需要在特定的日期范围(例如1月3日到3月5日)得到答案并且没有接听电话计数每小时(例如8:00到9:00),我不需要分开日期,只需要分开小时。

我已经使用过这个查询:

SELECT `event`, DATE(`time`) AS `date`, HOUR(`time`) AS `hour`, COUNT(*) AS `count` FROM `queue_log` WHERE `queuename` = 'Hozoori_Q' AND `event` IN ('CONNECT', 'RINGNOANSWER') AND `time` >= '2014-03-01' AND `time` <= '2014-03-05' GROUP BY `event`, `date`, `hour`;

“event”字段是呼叫状态。

此查询输出是这样的:

1 => 
array (size=4)
'event' => string 'CONNECT' (length=7)
'date' => string '2014-03-01' (length=10)
'hour' => string '9' (length=1)
'count' => string '99' (length=2)
2 => 
array (size=4)
'event' => string 'RINGNOANSWER' (length=7)
'date' => string '2014-03-01' (length=10)
'hour' => string '9' (length=1)
 'count' => string '5' (length=2)

但我希望是这样的:

  1 => 
    array (size=4)
      'date' => string '2014-03-01' (length=10)
      'hour' => string '9' (length=1)
      'answered_count' => string '99' (length=2)
      'not_answered_count' => string '5' (length=2)

有什么解决方案吗?

1 个答案:

答案 0 :(得分:1)

直接的SQL答案:

SELECT DATE(`time`) AS `date`, 
       HOUR(`time`) AS `hour`, 
       SUM(`event` = 'CONNECT') AS `answered_count`, 
       SUM(`event` = 'RINGNOANSWER') AS `not_answered_count` 
  FROM `queue_log` 
 WHERE `queuename` = 'Hozoori_Q' 
   AND `event` IN ('CONNECT', 'RINGNOANSWER') 
   AND `time` >= '2014-03-01' 
   AND `time` <= '2014-03-05'
 GROUP BY `date`, `hour`;