错误:You can't specify target table 'xxx' for update in FROM clause的解决

问题:  

  今天在MySQL数据库删除重复数据的时候遇到了一个问题。如下脚本:

DELETE FROM tempA WHERE tid IN (
SELECT MAX(tid) AS tid FROM tempA GROUP BY name,age
)

  会出现报错信息:

  You can‘t specify target table ‘tempA‘ for update in FROM clause

  大致意思是,在同一语句中,不能先select出同一表中的某些值,再update这个表。

解决方法:

  需要对上述脚本进行改造,如下:

DELETE FROM tempA WHERE tid NOT IN (
SELECT t.tid FROM (
SELECT MAX(tid) AS tid FROM tempA GROUP BY name,age
) t
)

  查询的时候增加一层中间表,就可以避免该错误。

错误:You can't specify target table 'xxx' for update in FROM clause的解决

时间: 2024-08-06 07:59:19

错误:You can't specify target table 'xxx' for update in FROM clause的解决的相关文章

mysql You can't specify target table 'xxx' for update in FROM clause的解决

DELETE from sp_goodscontent where goodsId in (SELECT t.goodsId from ( SELECT goodsId FROM sp_goodscontent GROUP BY goodsId HAVING count(1)>1 ) t) 之前的sql 查询,需要改成中间临时表再查询 mysql You can't specify target table 'xxx' for update in FROM clause的解决 原文地址:http

关于mysql 5.7版本“报[Err] 1093 - You can't specify target table 'XXX' for update in FROM clause”错误的bug

不同于oracle和sqlserver,mysql并不支持在更新某个表的数据时又查询了它,而查询的数据又做了更新的条件,因此我们需要使用如下的语句绕过: UPDATE teaching_department SET code_year = 2017, notice_code = (SELECT a.code + 1 FROM (SELECT MAX(notice_code) code FROM teaching_department WHERE department_id = 6284 and

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

1.执行sql语句报上面的错误: 1 DELETE 2 FROM 3 db_student 4 WHERE 5 RowGuid IN ( SELECT RowGuid FROM db_student WHERE age = 22 GROUP BY RowGuid HAVING count( * ) > 1 ) 6 AND ID NOT IN ( SELECT MAX( ID ) AS id FROM db_student WHERE age = 22 GROUP BY RowGuid HAVIN

[Django](1093, "You can't specify target table 'fee_details_invoices' for update in FROM clause") 错误

dele_id = Fee_details_invoices.objects.filter(fee_detail_id__in=fee_id_list, return_type='2').values_list('fee_detail_id', flat=True) Fee_details_invoices.objects.filter(fee_detail_id__in=dele_id).delete() 报错: django.db.utils.DatabaseError: (1093, "Y

(原)mysql错误1093 You can't specify target table 'wms_cabinet_form' for update in FROM clause

这个错误的意思是不能先select出同一表中的某些值,再update这个表(在同一语句中),解决方法不直接查询同一个,假设要更新的表为A,则先将A的的数据放到表B,再从表B中查询则得到更新和查询表A的效果 例: 成绩表: 把“SC”表中“姚明”老师教的课的成绩都更改为此课程的平均成绩 update sc set score=(select avg(a.score) FROM (select score,C from sc) a where a.C in (select C from course

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

mysql中不能这么用. (等待mysql升级吧)错误提示就是说,不能先select出同一表中的某些值,再update这个表(在同一语句中) 替 换方 案: create table tmp as select min(id) as col1 from blur_article group by title;delete from blur_article where id not in (select col1 from tmp); drop table tmp; 已经测试,尽请使用 You

mysql 一个较特殊的问题:You can't specify target table 'sys_user' for update in FROM clause

SELECT uin,account,password,create_user_uin_tree FROM sys_user 结果: 表中的create_user_uin_tree标识该条记录由谁创建. 创建新用户时,根据当前登录用户的uin及新创建的用户uin,有如下SQL: select concat(ifNULL(create_user_uin_tree,concat('_',2,'_')),'|_','97',"_") from sys_user where uin=2 结果:

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

开发执行时出现错误: update bm_area  set level = 2 where parent_code in (select b.parent_code from bm_area as b where b.level = 1); ERROR 1093 (HY000): You can't specify target table 'bm_area' for update in FROM clause 一般使用: create table tmp as select b.parent

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

做地址管理时,需要先根据要设为默认的地址的用户将用户的其他地址都设置为非默认 需要select出用户id然后update 原语句 update address set isdeafult = 0 where user_id = (select user_id from address where id = ?) 报错 -- You can't specify target table 'address' for update in FROM clause 大意是不能先select出同一表中的某些