2017-03-22 160 views
0

我有两个表,operationoperationTask。让我们说,operation只有选择具有相同外键的所有行,符合条件

  • ID

operationTask具有

  • ID
  • operation_id “为外键”
  • 状态 “布尔0:1”

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

我要选择所有操作,所有的他们的任务 “operationtask” status等于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 

operationtask:

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

的结果应该是:

操作:

ID 
--- 
2 
+0

什么是你的代码的问题? – Jens

+0

@Jens没有返回正确的结果 – Mohammad

+0

你能显示一些示例数据,其中包含预期的结果和结果 – Jens

回答

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