在一个查询中拥有多个MySQL Count
个实例或为每个计数实例分别进行查询是否更快?如果是这样,我将如何组合这两个查询?
$msd_call_qry = "SELECT count(event) AS MissedCalls FROM queuelogdb.queue_log WHERE (agent = '$agent_name[0]' OR agent = '$agent_name[1]')"
. " AND (event = 'ABANDON' OR event = 'RINGNOANSWER') AND queuename IN ('500','505') AND time BETWEEN '$lastDay' AND '$today'";
$ttl_call_qry = "SELECT count(event) AS TotalCalls from queuelogdb.queue_log WHERE (agent = '$agent_name[0]' OR agent = '$agent_name[1]') "
. "AND event = 'CONNECT' AND queuename IN ('500','505') AND time BETWEEN '$lastDay' AND '$today'";
答案 0 :(得分:2)
你的桌子是一样的。 WHERE
子句中的大多数条件都是相同的。所以,你应该能够使用条件聚合:
SELECT SUM(event = 'ABANDON' OR event = 'RINGNOANSWER') AS MissedCalls,
SUM(event = 'CONNECT') AS TotalCalls
FROM queuelogdb.queue_log
WHERE agent IN ('$agent_name[0]', '$agent_name[1]') AND
queuename IN ('500', '505') AND
time BETWEEN '$lastDay' AND '$today'". ";