MySql中不允许在Update/Delete中使用子查询引用操作目标表,怕引起一致性问题,如:

mysql> select id,name from t_sys_dept;
+----+--------------+
| id | name         |
+----+--------------+
|  1 | 总公司       |
| 12 | 省公司       |
| 13 | 一级渠道     |
| 14 | 二级渠道     |
+----+--------------+
4 rows in set (0.00 sec)mysql> delete from t_sys_dept where id=(select id from t_sys_dept where id=1);
ERROR 1093 (HY000): You can't specify target table 't_sys_dept' for update in FROM clause

直接报错ERROR 1093 (HY000): You can't specify target table 't_sys_dept' for update in FROM clause

官方给出了一种间接的方式,使用临时表,如下:

mysql> delete from t_sys_dept where id=(select id from (select id from t_sys_dept where id=1) a);
ERROR 1093 (HY000): You can't specify target table 't_sys_dept' for update in FROM clause

可以看到还是报错,这是因为MySql5.7以上,会进行sql优化,优化后的sql又触发了这个错误,有两种解决方式,一种是禁用优化,如:

mysql> SET optimizer_switch = 'derived_merge=off';
Query OK, 0 rows affected (0.00 sec)mysql> delete from t_sys_dept where id=(select id from (select id from t_sys_dept where id=14) a);
Query OK, 1 row affected (0.04 sec)

但这种方法有个问题是一条Sql变成了两条,在Mybatis中可使用添加allowMultiQueries=true的方式打开多条sql执行,但影响条数返回是有问题的,无法正常反映更新结果。

好在还有另外一种方式,在这里有提到,可以在子查询中使用DISTINCT或者添加LIMIT

mysql> SET optimizer_switch = 'derived_merge=on';
Query OK, 0 rows affected (0.00 sec)mysql> delete from t_sys_dept where id=(select id from (select id from t_sys_dept where id=13) a);
ERROR 1093 (HY000): You can't specify target table 't_sys_dept' for update in FROM clausemysql> delete from t_sys_dept where id=(select id from (select distinct id from t_sys_dept where id=13) a);
Query OK, 1 row affected (0.03 sec)

但使用临时表总是会影响性能,是否有必要用就需要做个评估了。

Mysql5.7 You can't specify target table 'table' for update in FROM clause相关推荐

  1. 错误: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 ...

  2. MySQL 语法问题:You can‘t specify target table ‘xxx‘ for update in FROM clause. 原因及解决方法

    报错信息如下: [Code: 1093, SQL State: HY000] You can't specify target table 'bd_bankaccbas' for update in ...

  3. You can't specify target table 'TS_AUTH_ADMIN' for update in FROM clause记录

    1. 报错:You can't specify target table 'TS_AUTH_ADMIN' for update in FROM clause, 百度查到说是,不能在同一语句中先sele ...

  4. [Err] 1093 - You can't specify target table 'xxx' for update in FROM clause解决方法

    执行开发同学提供的删除数据的sql时报错[Err] 1093 - You can't specify target table 'run_result' for update in FROM clau ...

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

    [Err] 1093 - You can't specify target table 's' for update in FROM clause 执行SQL DELETE from book WHE ...

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

    有时候我们在编辑update时需要select作为条件,在mysql中有时会出现这样的错误:You can't specify target table for update in FROM clau ...

  7. MySQL||SQL_ERROR_INFO: “You can‘t specify target table ‘titles_test‘ for update in FROM clause“

    一.问题描述 假定test数据库存在表titles_test,表内数据如下: 先要求删除emp_no重复的记录,只保留最小的id对应的记录.编写代码如下: delete from titles_tes ...

  8. mysql-1093 - You can‘t specify target table ‘titles_test‘ for update in FROM clause

    错误 DELETE FROM titles_test WHERE id NOT IN(SELECT MIN(id)FROM titles_testGROUP BY emp_no); 入上述操作会报错: ...

  9. mysql实战(五)—— You can‘t specify target table ‘org_department‘ for update in FROM clause

    系列文章目录 You can't specify target table 'org_department' for update in FROM clause 系列文章目录 背景 具体实现过程 1. ...

  10. MySQL [1093] You can‘t specify target table ‘titles_test‘ for update in FROM clause

    执行以下语句: delete from titles_test where id not in(select min(id)from titles_testgroup by emp_no); 会报出错 ...

最新文章

  1. 百度自动驾驶出租车服务在北京全面开放 可免费试乘
  2. 实验记录:vsftp整合mysql-pam管理虚拟账号
  3. WIN10 关闭驱动签名
  4. C指针原理(43)-helloworld的C程序汇编剖析
  5. 【Linux系统编程】进程同步与互斥:POSIX有名信号量
  6. 小白学jquery Mobile《构建跨平台APP:jQuery Mobile移动应用实战》连载四(场景切换)...
  7. 2019年终总结与新年重磅福利
  8. cogs2840. 二叉查找树
  9. YOLO系列专题——YOLOv2理论篇
  10. 2017.5.12PM
  11. Drool的LHS和RHS
  12. 【Unity】关于ScreenCapture.CaptureScreenshot截屏的尝试
  13. 雅马哈机器人编程讲解_雅马哈机器人配置方法之西门子S7-1500控制技巧分享
  14. 最受欢迎的30款开源软件
  15. WPJAM「网址导航」:最轻便快捷的WordPress网址导航插件
  16. r语言nonzerocoef函数_lars算法R语言操作指南.pdf
  17. 面试题:什么是野指针?产生野指针的原因?
  18. ARP攻击-流量分析
  19. 使用Dism++备份系统文件并恢复
  20. mysql 顿号_mysql语法总结及例子

热门文章

  1. JavaScript入门学习指南
  2. 时间字符串转Timestamp时间戳
  3. Docker的平行空间通信
  4. Java尚硅谷核心知识
  5. 新闻资讯类网站的PC端前台模板分享
  6. 使用抽象类阻止实例化
  7. Linux服务器连接校园网
  8. 【vs2019】vs2019(Visual Studio2019)离线安装包下载详细步骤
  9. Spring的动态代理原理
  10. 怎么用python抓取网页数据