I have three tables A
, B
, C
, and I want to randomly take a row from the col_b
column of the table B
then update it to the table A
. Table C
is the subtable of Table B
, which is used to filter the data of Table B
. Here is my sql statement:
update a a
set a.col_a_b =
(select t.col_b
from (select a1.col_a, b1.col_b, a1.rn_var
from (select a0.col_a, TRUNC(dbms_random.value(1, 6)) rn_var
from a a0) a1
left join (select b.col_b, rownum rn
from b b
where exists (select 1
from c c
where b.id = c.col_b_id
and c.col_c = 'c1')) b1
on a1.rn_var = b1.rn) t
where t.col_a = a.col_a);
I found a strange phenomenon:
-
If I remove
a1.rn_var
(linefrom (select a1.col_a, b1.col_b, a1.rn_var
), it doesn't work as my expected -
On the basis of the above, if I replace
exists
withleft join
(orjoin
), the result is the same -
If I reomve both
a1.rn_var
andexists
, it will work fine.
I know there may be a better way to implement it, but who can tell me why?
Aucun commentaire:
Enregistrer un commentaire