共計(jì) 2977 個(gè)字符,預(yù)計(jì)需要花費(fèi) 8 分鐘才能閱讀完成。
這期內(nèi)容當(dāng)中丸趣 TV 小編將會(huì)給大家?guī)?lái)有關(guān) oracle 和 mysql 關(guān)于關(guān)聯(lián)更新的差別有哪些,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
mysql 報(bào)錯(cuò) ERROR 1093 (HY000): You can t specify target table a for update in FROM clause
今天網(wǎng)站有些數(shù)據(jù)出現(xiàn)異常,需要把出現(xiàn)異常的數(shù)據(jù)更正,具體操作是把一個(gè)表中的 publish_date 字段關(guān)聯(lián)更新成同一個(gè)表中的 up_date 字段, 我們的數(shù)據(jù)是先存到 oracle,然后通過(guò)到 mysql 中的,所有異常數(shù)據(jù)存在于 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);
然后對(duì)應(yīng)著改寫(xiě)成 mysql 相應(yīng)表和相應(yīng) sql, 居然報(bào)錯(cuò)。。。。
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,把要更新的幾列數(shù)據(jù)查詢出來(lái)做為一個(gè)第三方表,然后篩選更新。
2,新建一個(gè)臨時(shí)表保存查詢出的數(shù)據(jù),然后篩選更新。最后刪除臨時(shí)表。
具體如下:
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
為了防止匹配不上,更新為空的問(wèn)題,可以加上 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);
如下是關(guān)于關(guān)聯(lián)更新的一些實(shí)驗(yàn):
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);
通過(guò)實(shí)驗(yàn) 1 和 2 比較可以知道,mysql 中是不能關(guān)聯(lián)更新同一個(gè)表的,但是 oracle 中可以;實(shí)驗(yàn) 4 和 5 可以知道 oracle 中不能使用 update a,b set a.=b. 之類的語(yǔ)句;實(shí)驗(yàn) 3 可以知道,mysql 可以使用 update a,b set a.=b. 之類的語(yǔ)句來(lái)關(guān)聯(lián)更新表;
上述就是丸趣 TV 小編為大家分享的 oracle 和 mysql 關(guān)于關(guān)聯(lián)更新的差別有哪些了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注丸趣 TV 行業(yè)資訊頻道。