[英]update with join in oracle sql
I want to update a column of a table1 but I should update only records where conditions are true in another table something like this, but I don't know how to implement this purpose in Oracle SQL我想更新 table1 的一列,但我应该只更新另一个表中条件为真的记录,就像这样,但我不知道如何在 Oracle SQL 中实现这个目的
update table1
join table2 on table1.msg_id = table2.id
set table1.index = table1.index-1
where table1.index > 10 and table2.type = 'myType'
I would write this as an exists
subquery in any database:我会把它写成任何数据库中的
exists
子查询:
update table1 t1
set index = t1.index - 1
where table1.index > 10 and
exists (select 1
from table2
where t2.id = t1.msg_id and
t2.type = 'myType'
);
The join
sort of implies that you are going to use data from table2
in the update of table1
. join
类型暗示您将在table1
的更新中使用table2
中的数据。 Instead, you simply want to change a value in a row when a particular condition is met.相反,您只想在满足特定条件时更改行中的值。
Oracle does not support this syntax (sigh). Oracle 不支持这种语法(叹气)。
For your use case, you could use a not exists
condition with a correlated subquery instead:对于您的用例,您可以将
not exists
条件与相关子查询一起使用:
update table1
set table1.index = table1.index - 1
where
table1.index > 10
and exists (
select 1 from table2 where table1.msg_id = table2.id and table2.type = 'mytype'
)
Note: make your live easier, do not use index
for a column name.注意:让您的生活更轻松,不要使用
index
作为列名。 This is a reserved work in pretty much all RDBMS.这是几乎所有 RDBMS 中的一项保留工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.