我有一个表CallTable
,其中包含名称为caller_id
和Is_picked_up
的列,其中包含的状态是否已被提取。
Caller_id Is_picked
1 no
1 yes
1 no
2 no
3 no
我希望从不接听电话的来电者。在上面的情况2和3将是输出。
答案 0 :(得分:3)
您可以group by
每个唯一Caller_id
并检查Is_picked
的列值是的条件计数是否为0:
select `Caller_id`
from `CallTable`
group by `Caller_id`
having sum(`Is_picked` = 'yes') = 0
<强> SQLFiddle Demo 强>
答案 1 :(得分:0)
您可以使用exists
运算符执行此操作:
SELECT DISTINCT caller_id
FROM call_table a
WHERE NOT EXISTS (SELECT *
FROM call_table b
WHERE a.caller_id = b.called_id AND is_picked = 'yes')
答案 2 :(得分:0)
使用子查询来获取至少有一个&#34;是&#34;在is_picked
列中并使用NOT IN
条件。
SELECT DISTINCT CALLER_ID FROM CallTable C1 where C1.CALLER_ID not in
(SELECT distinct CALLER_ID C2 FROM CallTable C2 having count(is_picked = "yes") > 1 );
SQLFiddle演示:http://sqlfiddle.com/#!9/ea507/13