MYSQL錯(cuò)誤代碼: 1093 You can't specify target table 'sc' for update in FROM clause

MYSQL執(zhí)行如下語(yǔ)句報(bào)錯(cuò):

UPDATE sc SET grade =grade*1.05 WHERE grade < (SELECT AVG(grade) AS avg_grade FROM sc)?

報(bào)錯(cuò)信息如下:

錯(cuò)誤代碼: 1093

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

意思是不能在同一語(yǔ)句中更新select出的同一張表元組的屬性值

解決方法:將select出的結(jié)果通過(guò)中間表再select一遍即可。

UPDATE sc SET grade =grade*1.05 WHERE grade < (SELECT avg_grade FROM (SELECT AVG(grade) AS avg_grade FROM sc) AS temp)

MYSQL手冊(cè)restrictions.html#subquery-restrictions里給出了限制規(guī)則和解決方法:

In general, you cannot modify a table and select from the same table in a subquery. For example, this limitation applies to statements of the following forms:

DELETE FROM t WHERE ... (SELECT ... FROM t ...);

UPDATE t ... WHERE col = (SELECT ... FROM t ...);

{INSERT|REPLACE} INTO t (SELECT ... FROM t ...);

Exception: The preceding prohibition does not apply if you are using a subquery for the modified table in theFROM?clause. Example:

UPDATE t ... WHERE col = (SELECT * FROM (SELECT ... FROM t...) AS _t ...);

Here the result from the subquery in theFROMclause is stored as a temporary table, so the relevant rows int?have already been selected by the time the update tottakes place.

其實(shí)想想也是這樣的粘我,對(duì)同一張表查的同時(shí)更新會(huì)引起數(shù)據(jù)不一致的問(wèn)題吧雹嗦,但是將查詢結(jié)果事先放到臨時(shí)表中就不會(huì)有這個(gè)問(wèn)題了。


這個(gè)是我們?cè)谑褂胾pdate或者delete語(yǔ)句時(shí),在where條件里面加入的子查詢導(dǎo)致的沾鳄。例如如下的update語(yǔ)句:

update table set type = 'static' where id in (

select id from ws_product where first_name ='superman'

);

修改上述語(yǔ)句為下面這樣踪宠,該問(wèn)題可以解決:

update ws_product set type = 'static' where id in (

select id form (

?select id from ws_product where first_name ='superman'

) xx

);

注意冲泥,這樣一定要給最里面的子查詢定義一個(gè)別名,不然會(huì)報(bào)另外一個(gè)錯(cuò)誤:

自己的例子:下面這種方式報(bào)錯(cuò) ? error code:1093

DELETE FROM Persons WHERE id NOT IN (SELECT MAX(id) FROM Persons GROUP BY NAME,age)

所以改成:

DELETE FROM Persons WHERE id NOT IN (SELECT * FROM (SELECT MAX(id) FROM Persons GROUP BY NAME,age) temp);

原因:一定要給最里面的子查詢定義一個(gè)別名.


錯(cuò)誤 :1093 You can’t specify target table ‘table name’ for update in FROM clause?

中文意思:不能先select出同一表中的某些值怀伦,再u(mài)pdate這個(gè)表(在同一語(yǔ)句中)

解決: 注:把同一個(gè)table重新包一遍脆烟,重命名

原句

updateperson_level_testsetcurrentStatus=0whereid=(selectmax(m.id)fromperson_level_test)

修改后

updateperson_level_testsetcurrentStatus=0whereid=(selectmax(m.id)from(select*fromperson_level_test) m)

轉(zhuǎn)自http://www.cnblogs.com/lteal/archive/2013/05/06/3062709.html


第一步:對(duì)aptop表進(jìn)行操作


操作沒(méi)有問(wèn)題,按照本思路對(duì)product表進(jìn)行操作

delete from tbl where id in (select max(id) from tbl a where EXISTS ( select 1 from tbl b where a.tac=b.tac group by tac HAVING count(1)>1) group by tac)

產(chǎn)生You can't specify target table '表名' for update in FROM clause錯(cuò)誤

3房待、解決思路:既然Mysql不讓對(duì)查詢到的目標(biāo)語(yǔ)句進(jìn)行更新邢羔,那么我在它的上面在套一個(gè)子查詢就可以。

將SELECT出的結(jié)果再通過(guò)中間表SELECT一遍吴攒,這樣就規(guī)避了錯(cuò)誤张抄。需要注意的是,這個(gè)問(wèn)題只出現(xiàn)于MySQL洼怔,MSSQL和Oracle不會(huì)出現(xiàn)此問(wèn)題署惯。

delete from tbl where id in (select a.id from (select max(id) id from tbl a where EXISTS (select 1 from tbl b where a.tac=b.tac group by tac HAVING count(1)>1)group by tac) as a)

update message set content='Hello World' where id in( select min_id from ( select min(id) as min_id from message group by uid) as a );

sql = "DELETE FROM qishu_books_sort08 WHERE Novel_url IN(SELECT * FROM(SELECT Novel_url FROM qishu_books_sort08 GROUP BY Novel_url HAVING count(Novel_url)>1 ORDER BY Novel_ID ASC) as tmp) AND Novel_ID NOT IN(SELECT * FROM(SELECT min(Novel_ID) FROM qishu_books_sort08 GROUP BY Novel_url HAVING count(Novel_url)>1 ORDER BY Novel_ID ASC) as tmp);"?


如下業(yè)務(wù)場(chǎng)景,ecs_order_shipping表里面記錄了每一個(gè)訂單的配送流轉(zhuǎn)記錄镣隶,type從11极谊,12,21安岂,22轻猖,23這么遞進(jìn).

按理說(shuō)每個(gè)訂單的每一個(gè)type就出現(xiàn)一次,因?yàn)橄到y(tǒng)bug造成type為11域那、12的記錄咙边,都出現(xiàn)了兩次或多次。對(duì)于每一個(gè)訂單,如果它有兩條type等于11的ecs_order_shipping記錄败许,那么只保留第一條王带,其他的刪除。

一市殷、先查出來(lái)那些要被刪除的記錄的id組合

#查出type為11的全部記錄

select * from ecs_order_shipping where type = 11;

#分組后愕撰,記錄少了,說(shuō)明有重復(fù)的記錄

select * from ecs_order_shipping where type = 11 group by order_sn;

#這樣篩選出那些重復(fù)的訂單

select * from ecs_order_shipping where type = 11 group by order_sn? having count(*) >1;

#從篩選的重復(fù)訂單中醋寝,得到id最大的

select max(id) from ecs_order_shipping where type=11 group by order_sn having count(*) >1;

二搞挣、根據(jù)這些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)

這樣就會(huì)報(bào)錯(cuò)R粜摺4呀啊!Error : You can't specify target table 'ecs_order_shipping' for update in FROM clause

三黄选、如上蝇摸,修改sql語(yǔ)句如下即可

/****每個(gè)訂單type等于11,12的記錄办陷,出現(xiàn)了很多重復(fù)的貌夕,要?jiǎng)h除重復(fù)項(xiàng),只留下最早的那個(gè)***/

delete from ecs_shipping where id in (select a.id from (select max(id) as id from ecs_shipping? as b where b.type=11? group by b.order_sn HAVING count(*)>1) as a );

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


mysql>delete from t where id in (select id from t where id < 5); ?

ERROR1093 (HY000): You can't specify target table 't' for update in FROM clause

改為下面就OK

delete from t where id in(select * from ((select id from t where id <5) tmp ); ?

主句(select * from (從句 temp)

刪除重復(fù)民镜,但保留最小id項(xiàng)啡专。

?DELETE FROM file WHERE? fileaddress IN (select * from ((SELECT? fileaddress FROM? file GROUP BY fileaddress HAVING count(fileaddress) > 1) a) )

AND id NOT IN (select * from ((SELECT min(id) FROM? file GROUP BY? fileaddress? HAVING? count(fileaddress) > 1? ?) b))? ?


刪除表中多余的重復(fù)記錄,重復(fù)記錄是根據(jù)單個(gè)字段(peopleId)來(lái)判斷制圈,只留有rowid最小的記錄

DELETE? FROM? people WHERE peopleName IN (SELECT peopleName FROM? people? GROUPBY? peopleName HAVING count (peopleName)>1)AND peopleId NOT IN ( SELECT min (peopleId) FROM? people GROUPBY? peopleName HAVING count (peopleName)>1);

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末们童,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子鲸鹦,更是在濱河造成了極大的恐慌慧库,老刑警劉巖,帶你破解...
    沈念sama閱讀 207,248評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件馋嗜,死亡現(xiàn)場(chǎng)離奇詭異齐板,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)葛菇,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,681評(píng)論 2 381
  • 文/潘曉璐 我一進(jìn)店門(mén)甘磨,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人眯停,你說(shuō)我怎么就攤上這事济舆。” “怎么了莺债?”我有些...
    開(kāi)封第一講書(shū)人閱讀 153,443評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵滋觉,是天一觀的道長(zhǎng)签夭。 經(jīng)常有香客問(wèn)我,道長(zhǎng)椎瘟,這世上最難降的妖魔是什么覆致? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 55,475評(píng)論 1 279
  • 正文 為了忘掉前任侄旬,我火速辦了婚禮肺蔚,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘儡羔。我一直安慰自己宣羊,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,458評(píng)論 5 374
  • 文/花漫 我一把揭開(kāi)白布汰蜘。 她就那樣靜靜地躺著仇冯,像睡著了一般。 火紅的嫁衣襯著肌膚如雪族操。 梳的紋絲不亂的頭發(fā)上苛坚,一...
    開(kāi)封第一講書(shū)人閱讀 49,185評(píng)論 1 284
  • 那天,我揣著相機(jī)與錄音色难,去河邊找鬼泼舱。 笑死,一個(gè)胖子當(dāng)著我的面吹牛枷莉,可吹牛的內(nèi)容都是我干的娇昙。 我是一名探鬼主播,決...
    沈念sama閱讀 38,451評(píng)論 3 401
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼笤妙,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼冒掌!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起蹲盘,我...
    開(kāi)封第一講書(shū)人閱讀 37,112評(píng)論 0 261
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤股毫,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后召衔,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體铃诬,經(jīng)...
    沈念sama閱讀 43,609評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,083評(píng)論 2 325
  • 正文 我和宋清朗相戀三年薄嫡,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了氧急。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,163評(píng)論 1 334
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡毫深,死狀恐怖吩坝,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情哑蔫,我是刑警寧澤钉寝,帶...
    沈念sama閱讀 33,803評(píng)論 4 323
  • 正文 年R本政府宣布弧呐,位于F島的核電站,受9級(jí)特大地震影響嵌纲,放射性物質(zhì)發(fā)生泄漏俘枫。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,357評(píng)論 3 307
  • 文/蒙蒙 一逮走、第九天 我趴在偏房一處隱蔽的房頂上張望鸠蚪。 院中可真熱鬧,春花似錦师溅、人聲如沸茅信。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,357評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)蘸鲸。三九已至,卻和暖如春窿锉,著一層夾襖步出監(jiān)牢的瞬間酌摇,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,590評(píng)論 1 261
  • 我被黑心中介騙來(lái)泰國(guó)打工嗡载, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留窑多,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 45,636評(píng)論 2 355
  • 正文 我出身青樓鼻疮,卻偏偏與公主長(zhǎng)得像怯伊,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子判沟,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,925評(píng)論 2 344

推薦閱讀更多精彩內(nèi)容