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

错误的意思说,不能先select出同一表中的某些值,再update这个表(在同一语句中)。 例如下面这个sql:

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
)

改写成下面就行了:

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

再如:UPDATE bk_ygsbk AS bk SET REPORT_VISIT = ( SELECT COUNT(*) FROM bk_ygsbk WHERE ZYID = ‘1‘ )  WHERE bk.ZYID = ‘1‘

应改成:UPDATE bk_ygsbk AS bk SET REPORT_VISIT = (SELECT * FROM ( SELECT COUNT(*) FROM bk_ygsbk WHERE ZYID = ‘1‘ ) AS r ) WHERE bk.ZYID = ‘1‘

也就是说将select出的结果再通过中间表select一遍,这样就规避了错误。注意,这个问题只出现于mysql,mssql和oracle不会出现此问题。

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

时间: 2024-08-13 16:21:46

MySQL: [Err] 1093 - You can't specify target table 'bk' for update in FROM clause的相关文章

关于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

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

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

delete from master_data where category_id not in (select category_id from master_data a, bc_category b where a.category_id=b.cate_id) 执行SQL语句时出现这个错误.原因是在更新这个表和数据时又查询了它,而查询的数据又做了更新的条件.以前ORACLE上面没有这个错误. 上面这个语句其实没有写好 不优化了 直接更新 delete from master_data wh

[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 't' for update in FROM clause

有一个表示地区的表,表结构与数据大概如下表. ID NAME PARENT_ID 1 中国 2 广东省 1 3 广州市 2 4 荔湾区 3 5 越秀区 3 6 番禺区 3 7 小谷围街道 6 现为了查询方便,需要加一列PARENT_NAME,用以表示上级地区的名称(虽然不符合第三范式,传递依赖,但有时为了业务上的可行性.便利性,可以按实际情况考虑) ID NAME PARENT_ID PARENT_NAME 1 中国 2 广东省 1 3 广州市 2 4 荔湾区 3 5 越秀区 3 6 番禺区 3

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 结果:

mysql 更新sql报错:You can't specify target table 'wms_cabinet_form' for update in FROM clause

数据库里面有两个字段的位置不对,要把他们对调换下.因为没有数据库写的权限,需要用sql语句来实现.原来以为简单的 update table a set a.字段a=(select b字段 from table  where id=?) ,set a.字段b=(select a字段 from table where id=?) where id=? ,结果报了 这个问题 You can't specify target table 'wms_cabinet_form' for update in

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出同一表中的某些