从表中的SELECT计数,其中列是CONNECT,但具有相关其他列的其他行没有特定值

时间:2014-03-10 20:32:23

标签: php mysql sql

我的MySQL数据库中有一个表,其中包含以下列:

  • CallId的
  • 事件

我想在以下查询中选择事件为'ENTERQUEUE'的所有行:

SELECT count(*)
FROM voipnow.ast_queue_log
WHERE queuename = '0536*401'
  AND DATE(time) = '2014-03-07'
  AND event = 'ENTERQUEUE'
ORDER BY time DESC;

但我只想显示事件列不等于CONNECT的行数,其中callid等于上述查询中的callid

2 个答案:

答案 0 :(得分:0)

尝试:

SELECT count(*)
  FROM voipnow.ast_queue_log
 WHERE queuename = '0536*401'
   AND DATE(time) = '2014-03-07'
   AND event <> 'CONNECT'
   and callid not in (SELECT callid
                        FROM voipnow.ast_queue_log
                       WHERE queuename = '0536*401'
                         AND DATE(time) = '2014-03-07'
                         AND event = 'ENTERQUEUE')
 ORDER BY time DESC;

答案 1 :(得分:0)

您的描述留有解释空间。我把它读成:
“没有行存在同一个callid和event ='CONNECT'”

SELECT count(*)
FROM   voipnow.ast_queue_log v1
WHERE  queuename = '0536*401'
AND    date(time) = '2014-03-07'
AND    event = 'ENTERQUEUE'
AND NOT EXISTS (
   SELECT 1
   FROM   voipnow.ast_queue_log v2
   WHERE  v2.callid = v1.callid 
   AND    v2.event = 'CONNECT'
   )