简体   繁体   English

更新通过联接在Oracle数据库中选择的行

[英]Update rows selected by join in Oracle database

I have the following query, in which I try to put everything so that I only need to execute the query once for optimization ( oracle 12c DBMS ). 我有以下查询,在其中尝试放置所有内容,以便仅执行一次查询即可进行优化( oracle 12c DBMS )。

update PersonStatus s
set s.status = 4
where exists (
    select 1 from PersonStatus s1
      inner join Person p
        on s1.id = p.id
    where p.details = 'california' and s1.status = 0 and s1.age in (1,2,3,4,5)
)

Tables: 表格:

Person (id, details)
PersonStatus(id, status, age)

where id in PersonStatus references id in Person. 其中PersonStatus中的id引用了Person中的id。 Note that I simplified/renamed the tables just for demo purposes. 请注意,我只是出于演示目的简化/重命名了表。

Basically, I just want to update the rows that match the condition in where exists (....), but I didn't seem to get it. 基本上,我只想更新存在条件(....)处与条件匹配的行,但似乎没有得到。

When I execute this, it updated all the rows in the table, but what I need is to update only those rows that have age in the given list (1,2,3,4,5). 当我执行此操作时,它更新了表中的所有行,但是我只需要更新在给定列表(1,2,3,4,5)中具有年龄的那些行。 This is for Oracle 12c database. 这是针对Oracle 12c数据库的。

Any idea why the behavior is like that? 知道为什么这种行为吗? Suggestions are appreciated. 建议表示赞赏。

===== in MySQL. =====在MySQL中。 my following query works fine: 我的以下查询工作正常:

update PersonStatus s
 inner join Person p on s.id = p.id
 set s.status = 4;
 where p.details = 'california' and s.status = 0 and s.age in (1,2,3,4,5)

I try to achieve this in Oracle 12c. 我尝试在Oracle 12c中实现这一目标。

I think the below modified query should work for you 我认为以下修改后的查询应该适合您

update PersonStatus s
   set s.status = 4
 where exists (select 1
                 from Person p
                where s.id = p.id
                  and p.details = 'california'
             )
  and s.status = 0
  and s.age in (1,2,3,4,5)
;

I think you just want a correlated subquery. 我认为您只需要一个相关的子查询。 In other words, you don't need PersonStatus in the subquery: 换句话说,您在子查询中不需要PersonStatus

update PersonStatus s
    set s.status = 4
    where exists (select 1
                  from Person p
                  where s.id = p.id and
                        p.details = 'california' and
                        s.status = 0 and s.age in (1,2,3,4,5)
                 );

I am guessing that this is the logic you are looking for. 我猜这是您要寻找的逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM