之前碰到一个sql语句,在oracle中可以运行,在mysql上就不行,后来查了好久,发现了他们两个联表批量更新数据的区别如下:
(借鉴一下别人的例子)
update :单表的更新不用说了,两者一样,主要说说多表的更新
复制代码 代码如下:
Oracle> Oracle的多表更新要求比较严格,所以有的时候不是很好写,我们可以试试Oracle的游标
update (
select t.charger_id_ new_charger_id_
from t_cus_year_status t
left join t_customer_infos cus on cus.id_ = t.cus_id_
where....
) n set n.new_charger_id_ =6;
复制代码 代码如下:
mysql>
update t_cus_year_status t
left join t_customer_infos cus on cus.id_ = t.cus_id_
set t.charger_id_ =6
where......;
然后仔细观察,可以看出,mysql的更新与oracle不同,是必须要先join on来进行联表,然后才set的,不然就会报语法错误。
附上我写的sql,是一个三表联查
UPDATE bm_crm_archive_info t join (selectDATE(now()) date_check,
t2.oper_per,t2.check_per,t1.pkg_code,t2.app_idfrom bm_crm_apply_rel_info t1 join bm_crm_archive_out_apply t2 on t1.app_id = t2.app_id) y on t.pkg_code =y.pkg_code
join bm_crm_archive_out_apply tt on y.app_id=tt.app_id and y.app_id = '4'
set t.out_per = y.oper_per,t.out_checker = y.check_per,t.out_date=y.date_check,t.status=4,tt.app_status=3
where y.app_id = '4' and y.app_id=tt.app_id