首先创建一个表:

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. MiniDao普通项目集成方案

    1.导入必要的jar包: 2.spring配置文件增加如下配置: <!-- Hibernate工具栏配置--> <bean id="miniDaoHiberCommonDa ...

  2. [leetcode] Min Stack @ Python

    原题地址:https://oj.leetcode.com/problems/min-stack/ 解题思路:开辟两个栈,一个栈是普通的栈,一个栈用来维护最小值的队列. 代码: class MinSta ...

  3. 数据导入读取read.table函数详解,如何读取不规则的数据(fill=T)

    函数 read.table 是读取矩形格子状数据最为便利的方式.因为实际可能遇到的情况比较多,所以预设了一些函数.这些函数调用了 read.table 但改变了它的一些默认参数. 注意,read.ta ...

  4. ImageEdit 展示图片(XAML, C#)

    <dxe:ImageEdit Source="/Gemr;component/Images/FakeUI/MedicalRecordFake.jpg" Stretch=&qu ...

  5. Windows XP 中设置VPN(PPTP连接方式)

    第一步:点开始-网上邻居或者控制面板-网络连接,选择-创建一个新的连接 第二步:点击-下一步 第三步:选择-连接到我的工作场所的网络,点击-下一步 第四步:选择-虚拟专用网络连接,点击-下一步 第五步 ...

  6. quick -- 添加按钮

    cc.ui.UIPushButton.new({ normal = "comm_btnGreenBackBack.png", pressed = "comm_btnGre ...

  7. asp 时间倒数后按钮可用

    <asp:Button runat="server" ID="btn" Text="免费获取验证码" onclick="bt ...

  8. git常用命令-基本操作

    git常用命令-基本操作 1)      新增文件 新增了Test_1.java git add Test_1.java git commit –m “新增了Test_1.java” git push ...

  9. 自己写的一个Yeoman的Generator-Require-Angularjs

    Yeoman是一个常见的工作流,能够很方面的搭建属于自己的脚手架. 这段时间我用闲暇时间写了一个Generator来玩了一下,这个Generator的主要目的是快速建立一个RequireJS+Angu ...

  10. WPF 设置程序开机自动运行(+注册表项)

    #region 设置程序开机自动运行(+注册表项) RegistryKey rgkRun = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Micr ...