JcR*_*R49 39 sql oracle sql-update
基于每个表中的一个列相等(user_id),将多个行从另一个表更新为一个表.
两个表都有一user_id
列.需要从数据插入t2
到t1
当user_id
列相等.
提前感谢您提供的任何帮助.
Dim*_*lov 50
update
table1 t1
set
(
t1.column1,
t1.column2
) = (
select
t2.column1,
t2.column2
from
table2 t2
where
t2.column1 = t1.column1
)
where exists (
select
null
from
table2 t2
where
t2.column1 = t1.column1
);
Run Code Online (Sandbox Code Playgroud)
或者这个(如果t2.column1 <=> t1.column1是多对一的,其中任何一个都是好的):
update
table1 t1
set
(
t1.column1,
t1.column2
) = (
select
t2.column1,
t2.column2
from
table2 t2
where
t2.column1 = t1.column1
and
rownum = 1
)
where exists (
select
null
from
table2 t2
where
t2.column1 = t1.column1
);
Run Code Online (Sandbox Code Playgroud)
Ton*_*ews 25
如果要使用t2中的数据更新t1中的匹配行,则:
update t1
set (c1, c2, c3) =
(select c1, c2, c3 from t2
where t2.user_id = t1.user_id)
where exists
(select * from t2
where t2.user_id = t1.user_id)
Run Code Online (Sandbox Code Playgroud)
"where exists"部分用于防止在不存在匹配的情况下将t1列更新为null.
sch*_*rik 14
merge into t2 t2
using (select * from t1) t1
on (t2.user_id = t1.user_id)
when matched then update
set
t2.c1 = t1.c1
, t2.c2 = t1.c2
Run Code Online (Sandbox Code Playgroud)
如果记录已存在于t1(user_id匹配)中,则它不是插入,除非您乐意创建重复的user_id.
您可能想要更新?
UPDATE t1
SET <t1.col_list> = (SELECT <t2.col_list>
FROM t2
WHERE t2.user_id = t1.user_id)
WHERE EXISTS
(SELECT 1
FROM t2
WHERE t1.user_id = t2.user_id);
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你...
归档时间: |
|
查看次数: |
195943 次 |
最近记录: |