查询count不存在的位置

时间:2015-10-06 06:44:20

标签: mysql sql

我从我拥有的两张桌子中随机取客,

customer
========
id
first_name
surname
country

call_log
====
id
customer_id #FK
create_by
create_datetime
status (status for No answer is 7)

截至目前,我需要在通话记录中排除有2个无应答状态计数的客户。我试过这个

SELECT
    customer.id, 
    customer.first_name,
    customer.country
FROM
    customer
WHERE
    NOT EXISTS
    (
        SELECT 1 #I dont know what I should do here
        FROM 
            call_log
        WHERE
            call_log.customer_id = customer.id AND
            call_log.status = 7 AND #no answer
            call_log.create_datetime BETWEEN '2015-10-06 00:00:00' AND '2015-10-06 23:59:59'
    )

非常感谢任何帮助。感谢。

2 个答案:

答案 0 :(得分:2)

让子选择返回no的数字。

SELECT
    customer.id, 
    customer.first_name,
    customer.country
FROM
    customer
WHERE (SELECT count(*)
       FROM call_log
       WHERE call_log.customer_id = customer.id AND
             call_log.status = 7 AND #no answer
             call_log.create_datetime BETWEEN '2015-10-06 00:00:00'
                                          AND '2015-10-06 23:59:59') < 2

答案 1 :(得分:0)

选择     顾客ID,     customer.first_name,     customer.country 从     顾客 WHERE(选择计数(*)        来自call_log        WHERE call_log.customer_id = customer.id AND              call_log.status = 7 AND #no answer              call_log.create_datetime BETWEEN&#39; 2015-10-06 00:00:00&#39;                                           AND&#39; 2015-10-06 23:59:59&#39;)&lt; 2