我有一个mysql查询,它按时间间隔计算调用次数(使用Select Case和Count)。我对每种类型的呼叫执行查询,在这种情况下是NP_call和其他呼叫。我正在尝试将我生成的数组组合成一个数组,其中包含每个时间间隔的调用计数。我在组合数组时遇到了困难。这是我将用于NP_calls的查询
SELECT CASE WHEN calldate between 08:00:00 and 08:29:59 then 08:00 (etc ...)
END AS interval
Count(channel) as NP_calls
FROM calls
WHERE cid like '%np%'
GROUP BY calldate
我的数组如下:$ arr3将是最终的组合数组:
$arr1 = Array ( [0] => Array ( [interval] => 07:00 [NP_calls] => 1)
[1] => Array ( [interval] => 08:00 [NP_calls] => 2)
[2] => Array ( [interval] => 08:30 [NP_calls] => 3)
[3] => Array ( [interval] => 10:00 [NP_calls] => 11)
[4] => Array ( [interval] => 11:00 [NP_calls] => 2))
$arr2 = Array ( [0] => Array ( [interval] => 07:00 [Other_calls] => 8)
[1] => Array ( [interval] => 07:30 [Other_calls] => 10)
[2] => Array ( [interval] => 09:00 [Other_calls] => 11)
[3] => Array ( [interval] => 10:00 [Other_calls] => 14)
[4] => Array ( [interval] => 10:30 [Other_calls] => 16))
$arr3 = Array ( [0] => Array ( [interval] => 07:00 [NP_calls] => 1
[Other_calls] => 8 )
[1] => Array ( [interval] => 07:30 [Other_calls] => 10 )
[2] => Array ( [interval] => 08:00 [NP_calls] => 2)
[3] => Array ( [interval] => 08:30 [NP_calls] => 3 )
[4] => Array ( [interval] => 09:00 [Other_calls] => 11 )
[5] => Array ( [interval] => 10:00 [NP_calls] => 11
[Other_calls] => 14 )
[6] => Array ( [interval] => 10:30 [Other_calls] => 16 )
[7] => Array ( [interval] => 11:00 [NP_calls] => 2)
在表格中,这是我想要得到的结果**通知在09:30没有任何内容,因为当时没有电话。
interval NP_calls Other_calls
07:00 1 8
07:30 10
08:00 2
08:30 3
09:00 11
10:00 11 14
10:30 16
11:00 2
如果有人能够在查询中弄清楚如何做得更好!
答案 0 :(得分:0)
在MySQL Side,您可以使用内部联接或左联接来组合表数据
http://www.w3schools.com/sql/sql_join_left.asp
http://www.w3schools.com/sql/sql_join_inner.asp
OR
在PHP端,您可以使用array_merge函数组合两个不同的数组
答案 1 :(得分:0)
你可以循环遍历两个数组并使用interval作为键创建组合数组,如下所示:
foreach ($arr1 as $np) {
$combined[$np['interval']]['NP_calls'] = $np['NP_calls'];
}
foreach ($arr2 as $other) {
$combined[$other['interval']]['Other_calls'] = $other['Other_calls'];
}
这将生成如下组合数组:
$combined = array(
'07:00' => array('NP_calls' => 1, 'Other_calls' => 8),
'07:30' => array('Other_calls' => 10),
'08:00' => array('NP_calls' => 2),
'08:30' => array('NP_calls' => 3),
'09:00' => array('Other_calls' => 11),
'10:00' => array('NP_calls' => 11, 'Other_calls' => 14),
'10:30' => array('Other_calls' => 16),
'11:00' => array('NP_calls' => 2)
);