oracle和mysql关于关联更新的一些差别以及ERROR 1093

mysql报错 ERROR 1093 (HY000): You can't specify target table 'a' for update in FROM clause
今天网站有些数据出现异常,需要把出现异常的数据更正,具体操作是把一个表中的publish_date字段关联更新成同一个表中的up_date字段,我们的数据是先存到oracle,然后通过到mysql中的,所有异常数据存在于oracle和mysql。
首先在oracle中更正:
SQL>update infoservice.t_publish_zbxx a set a.publish_date=(select b.up_date from infoservice.t_publish_zbxx b where a.record_id=b.record_id) where a.publish_date>to_date('2017-10-15','yyyy-mm-dd');
然后对应着改写成mysql相应表和相应sql,居然报错。。。。
mysql> update v_publish_info a set a.publish_date=(select b.up_date from v_publish_info b where a.id=b.id) where a.publish_date>'2017-07-15';
ERROR 1093 (HY000): You can't specify target table 'a' for update in FROM clause
解决办法:
mysql> update v_publish_info a,v_publish_info b set a.publish_date=b.up_date where a.id=b.id and a.publish_date>'2017-07-15';
或者
1,把要更新的几列数据查询出来做为一个第三方表,然后筛选更新。
2,新建一个临时表保存查询出的数据,然后筛选更新。最后删除临时表。
具体如下:
create table liuwenhe.publish_date_temp as select id ,publish_date,up_date from info.v_publish_info where publish_date>'2017-07-15';
update info.v_publish_info a set a.publish_date=(select b.up_date from liuwenhe.publish_date_temp b where a.id=b.id) where a.publish_date>'2017-07-15';
为了防止匹配不上,更新为空的问题,可以加上exists条件;
update info.v_publish_info a set a.publish_date=(select b.up_date from liuwenhe.publish_date_temp b where a.id=b.id) where a.publish_date>'2017-07-15' and exists (select b.up_date from liuwenhe.publish_date_temp b where a.id=b.id);
如下是关于关联更新的一些实验:
mysql:
1.成功
mysql> update liuwenhe.publish_date_20170715 a set a.publish_date=(select b.up_date from info.v_publish_info b where a.id=b.id) where a.publish_date>'2017-07-15' and exists (select b.up_date from info.v_publish_info b where a.id=b.id );
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 0

2.失败
mysql> update liuwenhe.publish_date_20170715 a set a.publish_date=(select b.up_date from liuwenhe.publish_date_20170715 b where a.id=b.id) where a.publish_date>'2017-07-15';
ERROR 1093 (HY000): You can't specify target table 'a' for update in FROM clause

3.成功
mysql> update liuwenhe.publish_date_20170715 a,liuwenhe.publish_date_20170715 b set a.publish_date=b.in_date where a.id=b.id and a.publish_date>'2017-07-15';
Query OK, 0 rows affected (0.01 sec)
Rows matched: 0 Changed: 0 Warnings: 0

oracle:
4.失败
SQL> update liuwenhe.top_80 a,infoservice.t_member_info b set a.login_id=b.login_id where a.member_id=b.record_id;
update liuwenhe.top_80 a,infoservice.t_member_info b set a.login_id=b.login_id where a.member_id=b.record_id
ERROR at line 1:
ORA-00971: missing SET keyword
5.失败:
SQL> update liuwenhe.top_80 a,liuwenhe.top_80 b set a.login_id=b.login_id where a.member_id=b.member_id;
update liuwenhe.top_80 a,liuwenhe.top_80 b set a.login_id=b.login_id where a.member_id=b.member_id
ERROR at line 1:
ORA-00971: missing SET keyword
6.成功
SQL>update liuwenhe.top_80 a set a.login_id=(select b.login_id from infoservice.t_member_info b where a.member_id=b.record_id);
总结:通过实验1和2比较可以知道,mysql中是不能关联更新同一个表的,但是oracle中可以;实验4和5可以知道oracle中不能使用update a,b set a.=b.之类的语句;实验3可以知道,mysql可以使用update a,b set a.=b.之类的语句来关联更新表;

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29654823/viewspace-2143509/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/29654823/viewspace-2143509/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值