我正在从我拥有的 2 个表中随机获取客户,
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'
)
非常感谢任何帮助。谢谢。
最佳答案
让子选择返回“否”的数量。
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
关于mysql - 使用 count 查询不存在的地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32963404/