首先创建一个表:

CREATE TABLE `t1` (
`id` INT(11) NULL DEFAULT NULL,
`name` VARCHAR(20) NULL DEFAULT NULL
)

插入几条数据:

mysql> select * from t1;
+------+------+
| id | name |
+------+------+
| 1 | chen |
| 2 | li |
| 3 | huan |
+------+------+
3 rows in set (0.00 sec)

需求1:删除最大id的那条记录,于是我们会大约写出如下的语句:

mysql> delete from t1 where id=(select max(id) from t1);
ERROR 1093 (HY000): You can't specify target table 't1' for update in FROM clause
很不幸,它报错了.

可以修改成如下语句:

mysql> delete a from t1 a,(select max(id) maxid from t1) b where a.id=b.maxid;
Query OK, 1 row affected (0.01 sec) mysql> select * from t1;
+------+------+
| id | name |
+------+------+
| 1 | chen |
| 2 | li |
+------+------+
2 rows in set (0.00 sec)

也可以是如下语句:

mysql> delete from t1 where id in ( select a.maxid from (select max(id) maxid from t1) a);
Query OK, 1 row affected (0.01 sec) mysql> select * from t1;
+------+------+
| id | name |
+------+------+
| 1 | chen |
+------+------+
1 row in set (0.00 sec)

需求2:插入一条记录,并且id值是之前该表最大值加1,于是我们会大约写出如下的语句:

mysql> insert into t1 values( (select max(id)+1 from t1),'you');
ERROR 1093 (HY000): You can't specify target table 't1' for update in FROM clause
依旧报了同样的错误

可以改写如下:

mysql> insert into t1 select (select max(id)+1 maxid from t1 ) , 'you';
Query OK, 1 row affected (0.06 sec)
Records: 1 Duplicates: 0 Warnings: 0 mysql> select * from t1;
+------+------+
| id | name |
+------+------+
| 1 | chen |
| 2 | you |
+------+------+
2 rows in set (0.00 sec)

需求3:我们要更新一条语句,id需要变为之前最大值加1,于是我们会大约写出如下的语句:

mysql> update t1 set id=(select max(id)+1 from t1) where id=1;
ERROR 1093 (HY000): You can't specify target table 't1' for update in FROM clause
错误如初

我们可以改写为如下语句:

mysql> update t1,(select max(id)+1 as maxid from t1 ) a set id=a.maxid where id=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from t1;
+------+------+
| id | name |
+------+------+
| 3 | chen |
| 2 | you |
+------+------+
2 rows in set (0.00 sec)

也可以改成如下语句:

mysql> update t1 set id=(select a.maxid from (select max(id)+1 maxid from t1) a) where id=3;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from t1;
+------+------+
| id | name |
+------+------+
| 4 | chen |
| 2 | you |
+------+------+
2 rows in set (0.00 sec)

总的思路是:把查询的最大值语句转为subquery或derived。

mysql: you can't specify target table 问题解决的更多相关文章

  1. mysql You can't specify target table for update in FROM clause解决方法

    mysql You can't specify target table for update in FROM clause解决方法出现这个错误的原因是不能在同一个sql语句中,先select同一个表 ...

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

    做地址管理时,需要先根据要设为默认的地址的用户将用户的其他地址都设置为非默认 需要select出用户id然后update 原语句 update address set isdeafult = 0 wh ...

  3. mysql You can't specify target table 'sys_org_relation' for update in FROM clause 删除表条件不能直接包含该表

    mysql中You can't specify target table for update in FROM clause错误的意思是说,不能先select出同一表中的某些值,再update这个表( ...

  4. 解决mysql You can't specify target table for update in FROM clause错误

    mysql中You can't specify target table for update in FROM clause错误的意思是说,不能先select出同一表中的某些值,再update这个表( ...

  5. Mysql - You can't specify target table '表名' for update in FROM clause 错误解决办法

    背景 在MySQL中,写SQL语句的时候 ,可能会遇到 You can't specify target table '表名' for update in FROM clause 这样的错误 错误含义 ...

  6. 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_goodsco ...

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

    含义:您不能在子句中为更新指定目标表'xxx'. 错误描述:删除语句中直接含select,如下: DELETE FROM meriadianannotation WHERE SeriesID IN ( ...

  8. mysql You can't specify target table 'sys_right_menu' for update in FROM clause (不能从Objor子句中指定目标表“SysRyType菜单)

    错误语句: DELETE from sys_right_menu where right_id  in (SELECT m.right_id from sys_right_menu  mLEFT JO ...

  9. Mysql You can't specify target table 'newsalrecord' for update in FROM clause

    这个问题是不能先select出同一表中的某些值,再update这个表(在同一语句中),即不能依据某字段值做判断再来更新某字段的值.解决办法就是建立个临时的表.

随机推荐

  1. Requirejs加载超时问题的一个解决方法:设置waitSeconds=0

    有时Requirejs会遇到加载js超时问题 除了排查js脚本问题,网络问题以外的一个解决方法是加大Require的等待时间waitSeconds,或者直接设置为0,这个参数的意义是:The numb ...

  2. ReactiveCocoa与Functional Reactive Programming

    转自 http://blog.leezhong.com/ios/2013/06/19/frp-reactivecocoa.html Functional Reactive Programming(以下 ...

  3. Scala 深入浅出实战经典 第61讲:Scala中隐式参数与隐式转换的联合使用实战详解及其在Spark中的应用源码解析

    王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-87讲)完整视频.PPT.代码下载: 百度云盘:http://pan.baidu.com/s/1c0noOt ...

  4. IE6下的效果

    1. IE6有宽度border实现透明 如果想使得边框颜色透明,在其余浏览器下比较简单,直接使用:border-color:transparent;但在IE6下这个办法不行,可以通过下面的方式实现: ...

  5. 《objective-c基础教程》学习笔记(九)—— Foundation框架介绍

    在之前的博文中,我们创建的项目文件的时候,默认都有引用#import <Foundation/foundation.h> 这个头文件.但是,之前我们对Foundation都没有展开介绍.这 ...

  6. 面向.Net程序员的后端性能优化实战

    最近2个月没做什么新项目 完全是对于旧的系统进行性能优化 避免超时 死锁 数据处理能力不够等常见的性能问题 这里不从架构方面出发 毕竟动大手脚成本比较高 那么我们以实例为前提 从细节开始 优化角度 一 ...

  7. JAVA本地远程连接linux程序监控状态

    环境:  1.本地window 2.程序部署在centos   一,启动访问权限安全守护程序 新建文件:jstatd.all.policy ,注意路径 grant codebase "$JA ...

  8. 【总结】编写自己的JDBC框架

    一.数据库连接池: 在一般用JDBC 进行连接数据库进行CRUD操作时,每一次都会: 通过:java.sql.Connection conn = DriverManager.getConnection ...

  9. BugTracker.NET的配置

    需求管理+任务管理+bug管理+看板管理 要求一定要简单,切忌不要太复杂 之前用的禅道的,功能虽然很强大,但是忒复杂了,用不下去. 几点需要注意的地方: 1.web.config里邮件的地方有好几个地 ...

  10. SAP ECC PP 配置文档

    SAP ECC 6.0 Configuration Document Production Planning & Control (PP) 1. General Settings 1.1 Ma ...