首先创建一个表:

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. C++ 记事本: 从历史说起

    C 的简史 在谈论 C++ 的历史那么必须先得了解 C 的历史,那么我们先来看一段来自于 <<C专家编程>> 对 C 语言史前阶段的简单阐述: Ken Thompson(左), ...

  2. ZookeeperNet太难用,写了个RetryHelper来进行配套使用

    普通的zk用法,如下写法: zk.Exists("/aaa", true); zk.Create(...); 但是由于这些API会抛Zookeeper的Exception,比如Co ...

  3. LogViewer - 方便的日志查看工具

    一个完整的程序日志记录功能是必不可少的,通过日志我们可以了解程序运行详情.错误信息等,以便更好的发现及解决问题. 日志可以记录到数据库.日志服务器.文件等地方,本文主要介绍文件日志. 文件日志通常是一 ...

  4. android 自定义日历控件

    日历控件View: /** * 日历控件 功能:获得点选的日期区间 * */ public class CalendarView extends View implements View.OnTouc ...

  5. chrome https添加信任

    在浏览器地址栏输入:chrome://net-internals/#hsts 然后到Add domain下,Domain添上诸如google.com和google.com.hk ,并勾选Include ...

  6. Intent用法简介

    Intent作为联系各Activity之间的纽带,其作用并不仅仅只限于简单的数据传递.通过其自带的属性,其实可以方便的完成很多较为复杂的操作.例如直接调用拨号功能.直接自动调用合适的程序打开不同类型的 ...

  7. jQuery插件实现图片展开效果,jquery.gallery。仿腾讯QQ空间说说图片展示效果。

    公司的项目http://www.umfun.com/,有个说说的页面(和腾讯QQ空间说说一样),里面有个发表图片功能,上传完图片,需要点击展开的效果. 当时手里面事情比较多(公司就我一个前端),忙不过 ...

  8. 《热血传奇2》wix、wil文件解析Java实现

    在百度上搜索java+wil只有iteye上一篇有丁点儿内容,不过他说的是错的!或者说是不完整的,我个人认为我对于热血传奇客户端解析还是有一定研究的,请移步: <JMir——Java版热血传奇2 ...

  9. [转]pycharm 2016 注册码

    pycharm 2016 注册码 复制 43B4A73YYJ-eyJsaWNlbnNlSWQiOiI0M0I0QTczWVlKIiwibGljZW5zZWVOYW1lIjoibGFuIHl1IiwiY ...

  10. 程序员之路:以Android证道

    大道三千,何以证道? 最近有私信.邮件给我咨询一些职业生涯规划的同学,我在这里以过来人的身份给大家一些建议. 任何行业,任何职位,无论高低,无论大小,都可以分为广博.精深两个方向. 精深自然指的是在某 ...