1. 程式人生 > >按條件刪除記錄時報You can’t specify target table for update in FROM clause錯誤解決方法(寫於20161107)

按條件刪除記錄時報You can’t specify target table for update in FROM clause錯誤解決方法(寫於20161107)

核心概念:
     mysql中,不能先select一個表的記錄,在按此條件進行更新和刪除同一個表的記錄。解決辦法是,將select得到的結果,再通過中間表select一遍,這樣就規避了錯誤,這個問題只出現於mysql,mssql和oracle不會出現此問題。

自己例項一:
如下業務場景,ecs_order_shipping表裡面記錄了每一個訂單的配送流轉記錄,type從11,12,21,22,23這麼遞進,
按理說每個訂單的每一個type就出現一次,因為系統bug造成type為11、12的記錄,都出現了兩次或多次。對於每一個訂單,如果它有兩條type等於11的ecs_order_shipping記錄,那麼只保留第一條,其他的刪除。
一、先查出來那些要被刪除的記錄的id組合
#查出type為11的全部記錄
select * from ecs_order_shipping where type = 11;
#分組後,記錄少了,說明有重複的記錄
select * from ecs_order_shipping where type = 11 group by order_sn;
#這樣篩選出那些重複的訂單
select * from ecs_order_shipping where type = 11 group by order_sn  having count(*) >1;
#從篩選的重複訂單中,得到id最大的
select max(id) from ecs_order_shipping where type=11 group by order_sn having count(*) >1;
二、根據這些id組合,刪除他們
delete from ecs_order_shipping where id in 
(
    select max(id) from ecs_order_shipping where type=11 group by order_sn having count(*) >1
)
這樣就會報錯!!!Error : You can't specify target table 'ecs_order_shipping' for update in FROM clause
三、如上,修改sql語句如下即可
/****每個訂單type等於11,12的記錄,出現了很多重複的,要刪除重複項,只留下最早的那個***/
delete from ecs_order_shipping where id in
(
    select a.id from
    (
        select max(id) as id from ecs_order_shipping as b where b.type=11  group by b.order_sn HAVING count(*) >1
    )as a
)

delete from ecs_order_shipping where id in
(
    select a.id from
    (
        select max(id) as id from ecs_order_shipping as b where b.type=12 group by b.order_sn HAVING count(*) >1
    )as a
)

相關推薦

條件刪除記錄時報You cant specify target table for update in FROM clause錯誤解決方法(20161107)

核心概念:     mysql中,不能先select一個表的記錄,在按此條件進行更新和刪除同一個表的記錄。解決辦法是,將select得到的結果,再通過中間表select一遍,這樣就規避了錯誤,這個問題只出現於mysql,mssql和oracle不會出現此問題。自己例項一:如下業務場景,ecs_order_sh

如何解決You cant specify target table for update in FROM clause錯誤

mysql中You can’t specify target table for update in FROM clause錯誤的意思是說,不能先select出同一表中的某些值,再update這個表(在同一語句中)。 例如下面這個sql:delete from target_info_day where id

MYSQL之You can't specify target table for update in FROM clause解決辦法

這篇文章主要介紹了mysql中You can’t specify target table for update in FROM clause錯誤解決方法,需要的朋友可以參考下 mysql中You can't specify target table for update

MySQL 出現You can't specify target table for update in FROM clause

MySQL出現You can’t specify target table for update in FROM clause 這個錯誤的意思是不能在同一個sql語句中,先select同一個表的某些值,然後再update這個表。 例如:message表儲存了多個使用者的訊息 建立表 CREATE TABLE

mysql修改刪除You can't specify target table for update in FROM clause的問題

you div code sql 語句 操作 查詢 重復數 -c sele 表中出現重復數據,需要刪除重復數據,只保留一條 DELETE FROM crm_participant WHERE id IN ( SELECT c.id cid F

MySQL:You can't specify target table for update in FROM clause

問題:You can't specify target table for update in FROM clause 含義:不能在同一表中查詢的資料作為同一表的更新資料。 注意:這個問題只出現於mysql,mssql和oracle不會出現此問題。 delete from people wher

MySQL 中 You can't specify target table '表名' for update in FROM clause錯誤解決辦法

在MySQL中,寫SQL語句的時候 ,可能會遇到You can't specify target table '表名' for update in FROM clause這樣的錯誤,它的意思是說,不能先select出同一表中的某些值,再update這個表(在同一語句中),即

關於mysql 5.7版本“報[Err] 1093 - You can't specify target table 'XXX' for update in FROM clause錯誤的bug

title _id fma xxx tps ice sql each targe 不同於oracle和sqlserver,mysql並不支持在更新某個表的數據時又查詢了它,而查詢的數據又做了更新的條件,因此我們需要使用如下的語句繞過: UPDATE teaching_de

錯誤You can't specify target table 'xxx' for update in FROM clause解決

參考:https://www.cnblogs.com/pcheng/p/4950383.html   解決:   程式碼: <!-- 執行"取消收藏" 操作 -> 根據前端傳入的商品id和sessio

MySQL 中 You can't specify target table '表名' for update in FROM clause錯誤

在MySQL中,寫SQL語句的時候 ,可能會遇到You can't specify target table '表名' for update in FROM clause這樣的錯誤,它的意思是說,不能先select出同一表中的某些值,再更新這個表(在同一語句中),即不能依據某

MySQL 錯誤碼: 1093 You can't specify target table 'jc_user' for update in FROM clause

MySQL 錯誤碼: 1093 You can’t specify target table ‘jc_user’ for update in FROM clause bug如何出現及解決方案 根據錯誤資訊可知:不能查詢一張表的同時修改同一張表

MySQL:You cant specify target table ‘A’ for update in FROM clause

按照MYSQL5.0文件的解釋:我們不能在修改表A的同時在其子查詢中使用到表A,但是可以通過在子查詢中在巢狀一層針對表A的子查詢,因為最裡層的子查詢產生的結果存在臨時表中,與表A沒有關係。 解決方法:把類似於 UPDATE t ... WHERE col = (SELE

You can't specify target table 't_mail_marketing' for update in FROM clause

update in table use pre RKE can stat mail date update t_mail_marketing set `STATUS` = 1 where ID in ( select b.PARENT_ID from (SELECT D

You can't specify target table 'table' for update in FROM clause

tar bsp stock select CA HA lec pan having delete from table1 where ID not in(select max(ID) ID from table1 group by row1) and row1 in

[Err] 1093 - You can't specify target table 'master_data' for update in FROM clause

sel master mce 沒有 AR delete 查詢 數據 select delete from master_data where category_id not in (select category_id from master_data a, bc_cate

You can't specify target table 't_open_user' for update in FROM clause

https://blog.csdn.net/jiangyu1013/article/details/79108498 報錯如題,意思大致是:在一條 sql 語句中不能先查出來部分內容,再同時又對當前表作修改。 解決方法:給查詢加別名,用中間表來實現不是對同一表作操作。 如錯誤定法

[Err] 1093 - You can't specify target table 's1_test' for update in FROM clause

前提說明:資料庫採用的是mysql。 資料庫表格: 題目: 刪除除了編號不同,其他資訊都相同的冗餘資訊。 思路: 1.找出除了編號不同,其他資訊不全相同的編號。 關鍵詞:group by……having :分組查詢,我對這個關鍵詞的理解是:不同的行之間找出列相同的一項或者

updateupdate中無法用基於被更新表的子查詢,You can't specify target table 'test1' for update in FROM clause.

子查詢 src nbsp spec tab can 技術分享 例如 bubuko update中無法用基於被更新表的子查詢,You can‘t specify target table ‘test1‘ for update in FROM clause. 情況如下: (

"svnserve: Can't bind server socket: Address already in use"報錯解決方法

問題原因 svn的埠衝突導致 當我們啟動svn服務後,系統會預設開啟的埠3690。當你啟動了一個版本庫後,再次啟動另外的版本庫,由於沒有指定埠號,這個版本庫還是會去使用3690埠,因此導致衝突。

MySQL解決報錯[Err] 1093 - You can't specify target...

做資料變更的時候經常遇到這樣的報錯 DELETE FROM ssq_contract_record WHERE LOAN_ID IN ( SELECT ssq_contract_record.LOAN_ID FROM ssq_contract_record