日期:2014-05-17  浏览次数:20846 次

oracle 多表更新问题
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表,后面又要关联一次(红色部分)呢?
之前都是用的sql server ,oracle是初学,不是很懂,求教了.

------解决方案--------------------
你做个试验 就明白了:确保把 值 更新到 你想更新的行上
------解决方案--------------------
我刚才看了 确实 是,没有 重复的 ,他们虽然 长得 差不多 吧,但是  意义是 不一样的,可能 你没看懂吧,这个以后,基本语法 差不多吧,和mysql ,sql server ,Oracle  就是 函数 不一样 了,基本 语法很接近的 。
------解决方案--------------------
如果不加条件,b表不存在与a表对应的记录话,那么a表被更新字段就会被更新为空。
------解决方案--------------------
没有where exists 的话a表有的记录但b表没有的记录,a表的city_name更新为空,
有where exists 就会保留a表中原来的记录。