mysql - 选择具有相同外键的所有行,满足条件

标签 mysql sql

我有两个表,operationoperationTask。假设操作只有

  • 身份证

操作任务

  • 身份证
  • operation_id“作为外键”
  • 状态“ bool 值 0:1”

这两个表之间的关系是一对多。

我想选择所有任务“operationtask”状态都等于 1 的所有操作。

我尝试过的:

SELECT * 
FROM `operation` 
WHERE operation.id = All(
    SELECT task.operation_id 
    FROM operationtask task 
    WHERE task.status=1 
    GROUP BY task.operation_id)

例如:

操作:

ID
---
1
2
3

操作任务:

ID   operation_id   status
---  ------------   ------
1         1           1
2         1           0
3         2           1
4         2           1
5         3           0
6         3           0

结果应该是:

操作:

ID
---
2

最佳答案

select *
from operations o
where not exists (
      select 1 
      from operationtask t 
      where t.operation_id = o.id and t.status = 0)

关于mysql - 选择具有相同外键的所有行,满足条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42946237/

相关文章:

php - 使用括号使用 mysql 查询未检索到正确的结果

sql - 使用变量获取 SQL xml 属性值

mysql - 如何从 MySql 中的子查询以文本形式返回列值?

mysql - 带有子查询的 SQL 查询

mysql - 启动 ror 应用程序时无法连接到 mysql

mysql - 如何在考虑权重的情况下随机选择一行?

mysql - having 子句中的最大计数多个最大值

php - 如何在紧急情况下阻止站点的所有用户?

mysql - 根据具有不同值的顺序连接两列

php - wordpress - 按字母显示类别的书签链接