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是初学,不是很懂,求教了.

解决方案 »

  1.   

    一个是在值的位置,是用来查值的,一个在where位置,是用来过滤数据的,什么叫“又写了一遍”
      

  2.   

    where exists (select 1
    from tmp_cust_city b
    where b.customer_id=a.customer_id这段?
    指的是b 表存在这个结果集,确定update的范围
      

  3.   

    我刚才看了 确实 是,没有 重复的 ,他们虽然 长得 差不多 吧,但是  意义是 不一样的,可能 你没看懂吧,这个以后,基本语法 差不多吧,和mysql ,sql server ,Oracle  就是 函数 不一样 了,基本 语法很接近的 。
      

  4.   

    如果不加条件,b表不存在与a表对应的记录话,那么a表被更新字段就会被更新为空。
      

  5.   

    没有where exists 的话a表有的记录但b表没有的记录,a表的city_name更新为空,
    有where exists 就会保留a表中原来的记录。