我有一个如下表
table_fruit:
account_id fruit
1 apple
1 banana
2 apple
2 orange
2 banana
3 apple
我正在尝试列出每个拥有苹果而没有其他水果的人的名单
这似乎是一个简单的想法,但它现在就超越了我......
最佳答案
您可以使用不存在
作为
select * from table_fruit t1
where t1.fruit = 'apple'
and not exists(
select 1 from table_fruit t2
where t2.account_id = t1.account_id
and t2.fruit <> 'apple'
);
关于mysql - sql语句选择约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28834083/