我有两个表,operation
和 operationTask
。假设操作
只有
- 身份证
和操作任务
有
- 身份证
- 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/