【问题标题】:Updating rows in a table using join and group by in Oracle produces an ORA-01732 error “data manipulation operation not legal on this view”在 Oracle 中使用 join 和 group by 更新表中的行会产生 ORA-01732 错误“数据操作操作在此视图上不合法”
【发布时间】:2020-05-13 16:42:21
【问题描述】:

我需要使用 join 和 group by 语句更新表中的一些行。这是我的 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';

由于“分组依据”语句,这似乎是不可能的,因为我收到以下错误消息:

ORA-01732:“此视图上的数据操作操作不合法”

谁能帮我找到另一种方法来做到这一点?

【问题讨论】:

  • 请提供样本数据和期望的结果。 full joingroup byupdate 中都没有意义。目前还不清楚您要做什么。

标签: sql oracle group-by inner-join


【解决方案1】:

您似乎在进行计数以确定table2 中没有出现匹配的行;在这种情况下,您可以改用not exists (doc) 来确定:

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 行。

【讨论】:

  • 谢谢亚历克斯。这正是我想要做的。您的解决方案非常适合我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-02
  • 2019-01-27
  • 1970-01-01
  • 2012-08-19
  • 1970-01-01
相关资源
最近更新 更多