我对SQL甚至是一般的数据库都是新手,所以我不确定这是否是特定于SQL的问题,但无论如何这就是我所拥有的:
3表:
项目
pid
...
finished
Project_Actions
pid
aid
操作
aid
...
finished
如果是必需的信息:
pid = project ID
aid = action ID
One action can belong to >=0 projects
现在,我想在项目表中设置所有相应操作已完成的项目。
我遇到的问题是我完全没有想法,如何在实际更新条目之前检查所有操作。
任何帮助将不胜感激。
答案 0 :(得分:0)
您可以选择像这样完成的pid
select p.pid
from projects p
join project_actions pa on pa.pid = p.pid
join actions a on pa.pid = a.pid
group by p.pid
having sum(case when p.finished = 0 then 1 else 0 end) = 0
如果您想更新表格,可以使用
等查询update projects
set some_column = 'some value'
where pid in
(
select p.pid
from projects p
join project_actions pa on pa.pid = p.pid
join actions a on pa.pid = a.pid
group by p.pid
having sum(case when p.finished = 0 then 1 else 0 end) = 0
)
在MySQL中,您无法更新您选择的同一个表。所以你需要一个像这样的临时表
update projects
set some_column = 'some value'
where pid in
(
select * from
(
select p.pid
from projects p
join project_actions pa on pa.pid = p.pid
join actions a on pa.pid = a.pid
group by p.pid
having sum(case when p.finished = 0 then 1 else 0 end) = 0
) tmp
)