[英]Update rows selected by join in Oracle database
我有以下查詢,在其中嘗試放置所有內容,以便僅執行一次查詢即可進行優化( 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)
)
表格:
Person (id, details)
PersonStatus(id, status, age)
其中PersonStatus中的id引用了Person中的id。 請注意,我只是出於演示目的簡化/重命名了表。
基本上,我只想更新存在條件(....)處與條件匹配的行,但似乎沒有得到。
當我執行此操作時,它更新了表中的所有行,但是我只需要更新在給定列表(1,2,3,4,5)中具有年齡的那些行。 這是針對Oracle 12c數據庫的。
知道為什么這種行為嗎? 建議表示贊賞。
=====在MySQL中。 我的以下查詢工作正常:
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)
我嘗試在Oracle 12c中實現這一目標。
我認為以下修改后的查詢應該適合您
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)
;
我認為您只需要一個相關的子查詢。 換句話說,您在子查詢中不需要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)
);
我猜這是您要尋找的邏輯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.