Sql Oracle:基于条件的更新
我想根据条件更新表的一列。它应该检查该值是否存在于其他表中,如果存在,则将使用其他表中的值,否则将使用相同表中的值Sql Oracle:基于条件的更新,sql,oracle,sql-update,Sql,Oracle,Sql Update,我想根据条件更新表的一列。它应该检查该值是否存在于其他表中,如果存在,则将使用其他表中的值,否则将使用相同表中的值 Update table1 Set column1=(select t2.alias||’@email.com’ as new_name From table2 t2, table1 t1, table3 t3 Where t1.id=t2.id And t1.id=t3.id Else if Select t2.alias is null or t2.id is null t
Update table1
Set column1=(select t2.alias||’@email.com’ as new_name
From table2 t2, table1 t1, table3 t3
Where t1.id=t2.id
And t1.id=t3.id
Else if
Select t2.alias is null or t2.id is null
then column1= select t1.id||’@email.com’ as new_name
对此有什么建议吗
提前谢谢 这是你想要的吗
Update table1
Set column1 = (select (case when t2.alias is not null and t2.id is not null then t2.alias
else t1.id
end) ||'@email.com' as new_name
From table1 t1 left outer join
table2 t2
on t1.id=t2.id
);
我删除了表3,因为它似乎没有被使用。使用left join is甚至不会过滤任何结果。通过省略merge语句的insert半部分,您可以将其变成严格的update语句:
MERGE INTO table1 t
USING(
SELECT t2.id, t2.Alias
FROM table2 t2
JOIN table1 t1
ON t1.id = t2.id
AND t1.Alias <> t2.Alias
) tu
on( tu.id = t.id )
WHEN MATCHED THEN
update set t.Alias = tu.Alias;
查询将只返回表2中与表1不同的现有值。为了使它与您的需求完全匹配,如果表2中存在任何值,请更新表1,然后删除该行和t1.Alias t2.Alias,但是为什么要将字段更新为它已经具有的相同值呢