简体   繁体   中英

Update a column in a table and use inner join in Oracle

I want to update one column in a table, but I want to add a table with join and use a condition in other table. This is my code but it is not correct - can someone help me with this? I am using Oracle dbms

update table_person 
inner join table_person_info on table_person.person_id = table_person_info.person_id 
set table_person.gender_type_id = 98 
where table_person_info.type_id = 51 and table_person.gender_type_id = 97;

INNER JOIN in update is not allowed in Oracle. You can use EXISTS as follows.

update table_person tp
  set tp.gender_type_id = 98
where exists 
      (select 1 from table_person_info tpi where tp.person_id = tpi.person_id 
       and tpi.type_id = 51)
 and tp.gender_type_id = 97;

you should use '''exists''' instead of inner join

you should not use inner join but you can use this code if you want to use it

update 
(select t1.gender_type_id 
from table_person t1 inner join table_person_info t2 on t1.person_id = t2.person_id 
where t2.type_id = 51)
set gender_type_id = 98 where gender_type_id = 97

First you should build a true table with these two table and then you can update your column

update 
(select t1.gender_type_id 
from table_person t1 inner join table_person_info t2 on t1.person_id = t2.person_id 
where t2.type_id = 51)
set gender_type_id = 98 where gender_type_id = 97

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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