失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > oracle两条update语句怎么写 Oracle两表关联执行update语句代码

oracle两条update语句怎么写 Oracle两表关联执行update语句代码

时间:2019-07-18 03:54:28

相关推荐

oracle两条update语句怎么写 Oracle两表关联执行update语句代码

Oracle两表关联执行update时,因为没有像SqlServer的update from,因此要麻烦一些,通常有以下四种方式:

第一种:更新的条件为两个表的查询关联

update customers a – 使用别名

set customer_type=’01′ –01 为vip,00为普通

where exists (select 1

from tmp_cust_city b

where b.customer_id=a.customer_id

)

第二种:更新的条件为两个表的查询关联,值为查询值

update customers a — 使用别名

set city_name=(select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id)

where exists (select 1

from tmp_cust_city b

where b.customer_id=a.customer_id

)

第三种: update 超过2个值

update customers a — 使用别名

set (city_name,customer_type)=(select b.city_name,b.customer_type

from tmp_cust_city b

where b.customer_id=a.customer_id)

where exists (select 1

from tmp_cust_city b

where b.customer_id=a.customer_id

)

第四种,使用视图方式更新,这样能避免对B表或其索引的2次扫描,但前提是 A(customer_id) b(customer_id)必需是unique index

或primary key。否则报错:

update (select a.city_name,b.city_name as new_name

from customers a,

tmp_cust_city b

where b.customer_id=a.customer_id

)

set city_name=new_name

如果觉得《oracle两条update语句怎么写 Oracle两表关联执行update语句代码》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。