您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關oracle和mysql關于關聯更新的差別有哪些,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
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.之類的語句來關聯更新表;
上述就是小編為大家分享的oracle和mysql關于關聯更新的差別有哪些了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。