繁体   English   中英

选择所有具有相同外键的行都满足条件的位置

[英]select where all rows having same foreign key, meet the condition

我有两个表, operationoperationTask 假设operation只有

  • ID

operationTask

  • ID
  • operation_id“作为外键”
  • 状态为“布尔值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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM