我需要使用连接和分组语句更新一个表中的一些行。这是我的SQL脚本。
update (select t1.name, t1.status as newStatus , count(table2.id) from table1 t1
full outer join table2 t2 on (t1.id = t2.table1_id)
where t1.status = 'Old Status'
group by t1.name, t1.status
having count(t2.id) = 0) res
set res.newStatus = 'New Status';
似乎由于 "Group by "语句的原因,这是不可能的,因为我得到了以下错误信息。
ORA-01732: "数据处理操作在此视图上不合法"
谁能帮我找到其他方法来做?
你似乎是在做计数,以确定没有匹配的记录出现在 table2
;在这种情况下,您可以使用 not exists
(文档)来确定。
update table1 t1
set t1.status = 'New Status'
where t1.status = 'Old Status'
and not exists (
select *
from table2 t2
where t2.table1_id = t1.id
);
你原来的语句使用了一个完整的外连接,这也没有什么意义,因为你需要一个... ... table1
要更新的行。