MYSQL错误代码: 1093 You can't specify target table 'sc' for update in FROM clause

MYSQL执行如下语句报错:

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

报错信息如下:

错误代码: 1093

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

意思是不能在同一语句中更新select出的同一张表元组的属性值

解决方法:将select出的结果通过中间表再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手册restrictions.html#subquery-restrictions里给出了限制规则和解决方法:

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.

其实想想也是这样的,对同一张表查的同时更新会引起数据不一致的问题吧,但是将查询结果事先放到临时表中就不会有这个问题了。


这个是我们在使用update或者delete语句时,在where条件里面加入的子查询导致的。例如如下的update语句:

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

select id from ws_product where first_name ='superman'

);

修改上述语句为下面这样,该问题可以解决:

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

select id form (

 select id from ws_product where first_name ='superman'

) xx

);

注意,这样一定要给最里面的子查询定义一个别名,不然会报另外一个错误:

自己的例子:下面这种方式报错   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);

原因:一定要给最里面的子查询定义一个别名.


错误 :1093 You can’t specify target table ‘table name’ for update in FROM clause 

中文意思:不能先select出同一表中的某些值,再update这个表(在同一语句中)

解决: 注:把同一个table重新包一遍,重命名

原句

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

修改后

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

转自http://www.cnblogs.com/lteal/archive/2013/05/06/3062709.html


第一步:对aptop表进行操作


操作没有问题,按照本思路对product表进行操作

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)

产生You can't specify target table '表名' for update in FROM clause错误

3、解决思路:既然Mysql不让对查询到的目标语句进行更新,那么我在它的上面在套一个子查询就可以。

将SELECT出的结果再通过中间表SELECT一遍,这样就规避了错误。需要注意的是,这个问题只出现于MySQL,MSSQL和Oracle不会出现此问题。

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);" 


如下业务场景,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_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)

删除重复,但保留最小id项。

 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))   


删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有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);

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,222评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,455评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,720评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,568评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,696评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,879评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,028评论 3 409
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,773评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,220评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,550评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,697评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,360评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,002评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,782评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,010评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,433评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,587评论 2 350

推荐阅读更多精彩内容