简体   繁体   中英

oracle update two based on 2 condition from another table

I have 2 table. TableA and TableB

I want to update TableA based on 2 condition, 1 condition is from TableA and second condition is from TableB .

UPDATE TableA SET
    TYPE = (CASE when TYPE = (SELECT DISTINCT(A.TYPE) FROM TableA A JOIN TableB B ON B.SOID = A.NAME where B.level = 9 and A.TYPE = 66) then 12
                      when TYPE = (SELECT DISTINCT(A.TYPE) FROM TableA A JOIN TableB B ON B.SOID = A.NAME where B.level = 4 and A.TYPE = 66) then 11
             else NULL 
END);

for some reason it is updating not correctly. I only have 3 records that meet this condition but it is updating every row it looks like.

This can done in SQL using The syntax for the SQL UPDATE statement when updating multiple tables (not permitted in Oracle) is:

UPDATE table1, table2, ... 
SET column1 = expression1,
    column2 = expression2,
    ...
WHERE table1.column = table2.column
AND conditions;

After Lots and Lots of Google.

I found the solution. Hope it helps others.

    UPDATE (SELECT A.TYPE,B.LEVEL FROM TABLEA A 
            JOIN TABLEB B ON B.SOID = A.NAME where B.level IN (9,4) and A.TYPE = 66) 
    SET TYPE = (CASE (WHEN LEVEL = 9 THEN 12 
                  WHEN LEVEL = 4 THEN 11 
                  ELSE NULL 
                END);

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