源自:http://blog.youkuaiyun.com/luck901229/article/details/8451432
假设表a中有多个字段(province ,city)需要从b表获取(两张表的mobile一样),总结了几种写法。
一、update a set a.province=(select province from b where b.mobile=a.mobile);
update a set a.city=(select cityfrom b where b.mobile=a.mobile);
这种写法效率太低,尤其是号码有上万条的时候,所以抛弃。
二、update a set a.province=b.province,a.city=b.city from a inner join b on a.mobile=b.mobile.
或者update a set a.province=b.province,a.city=b.city from a,b where a.mobile=b.mobile.
三、update a inner join b on a.mobile=b.mobile set a.province=b.province,a.city=b.city
注意:第二种和第三种写法在oracle行不通的,老是报错,折腾了好长时间,最后还是用下面的语句解决了问题
四、update a set(a.province,a.city)=(select province,city from b where b.mobile=a.mobile)
其实第四种方法是第一种方法的合并。
项目中写的真实例子:
update m_smsphoneno a set (a.operator,a.province,a.city)=(select OWNER,STATE,CITY from keyaccount.CELLPHONESORT b where substr(a.mobile,1,7)=b.startcode) where a.category=2 and a.city is null; 注:用a.city=null不行的
-
2楼
灰尘ve 2015-04-24 08:59发表 [回复]
-
-
第一种遍历整张表遍历了两遍,第四种遍历整张表遍历了一遍。
不过子查询查出的a.operator,a.province,a.city结果为空,那么就会把a表的对应数据插入成空。