[英]select where all rows having same foreign key, meet the condition
我有两个表, operation
和operationTask
。 假设operation
只有
和operationTask
有
这两个表之间的关系是一对多的。
我要选择所有任务“ 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.