Oracle SQL correlated update

前端 未结 4 745
生来不讨喜
生来不讨喜 2021-01-21 23:45

I got three tables:

t1.columns: a,c
t2.columns: a,b
t3.columns: b,c,d

Now what I want is to update t1.c with t3.d. But I can\'t just update t1

4条回答
  •  死守一世寂寞
    2021-01-22 00:11

    If there is a one-to-many relationship between t1 and t2 or between t2 and t3 you will get many matches for each row in t1. If you know that all rows in t3 that belong to the same row in t1 have the same value in d, then you can use DISTINCT to remove (identical) duplicates.

    UPDATE table1 t1
       SET t1.c = (select DISTINCT t3.d
                   from table2 t2, table3 t3
                   where t2.b = t3.b and t1.a = t2.a)                                  
     WHERE EXISTS ( SELECT 1 FROM table2 t2, table3 t3 WHERE t1.c = t3.c and t1.a = t2.a);
    

提交回复
热议问题