mysql - sql语句选择约束

标签 mysql

我有一个如下表

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/

相关文章:

php - HTML 净化器在插入 mysql 之前无法使用 $_POST

mysql - 有没有办法识别那些在 where IN() 语句中找不到的记录?

php - 为 codeIgniter ErrorException [8192] : mysql_real_escape_string() 设置数据库

php - 在 PHP7+MySQL 中从 SHA256 哈希密码迁移到 BCRYPT

php - 用于记录尝试插入已存在行的列

python - GAE Python - 没有名为 MySQLdb 的模块

mysql - 内连接 - Mysql 和 PHP

php 输出不显示第一项详细信息和错误

php - 如何通过序列化形式从不同的输入名称中获取值以插入到mysql中?

mysql - 在插入触发器后将数据插入其他表