mysql: you can't specify target table 问题解决
首先创建一个表:
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 问题解决的更多相关文章
- 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同一个表 ...
- Mysql -- You can't specify target table 'address' for update in FROM clause
做地址管理时,需要先根据要设为默认的地址的用户将用户的其他地址都设置为非默认 需要select出用户id然后update 原语句 update address set isdeafult = 0 wh ...
- 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这个表( ...
- 解决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这个表( ...
- 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 这样的错误 错误含义 ...
- 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 ...
- mysql You can't specify target table 'xxx' for update in FROM clause
含义:您不能在子句中为更新指定目标表'xxx'. 错误描述:删除语句中直接含select,如下: DELETE FROM meriadianannotation WHERE SeriesID IN ( ...
- 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 ...
- Mysql You can't specify target table 'newsalrecord' for update in FROM clause
这个问题是不能先select出同一表中的某些值,再update这个表(在同一语句中),即不能依据某字段值做判断再来更新某字段的值.解决办法就是建立个临时的表.
随机推荐
- IPv6正则表达式
斯蒂芬·瑞恩写了一个非常有用的正则表达式,可用于匹配任何一个合法的IPv6地址.以下为正则表达式的代码: ^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|: ...
- 奇怪吸引子---TreeScrollUnifiedChaoticSystem
奇怪吸引子是混沌学的重要组成理论,用于演化过程的终极状态,具有如下特征:终极性.稳定性.吸引性.吸引子是一个数学概念,描写运动的收敛类型.它是指这样的一个集合,当时间趋于无穷大时,在任何一个有界集上出 ...
- Intrinsics头文件与SIMD指令集、Visual Studio版本对应表(转)
File:Intrinsics头文件 描述:指令集描述VS:Visual Studio版本号VisualStudio:Visual Studio版本名 File 描述 VS VisualStudio ...
- WPF窗体的命令绑定
方法一:使用代码 <WpfUI:View.CommandBindings> <CommandBinding Command="Help" CanExecute=& ...
- 最新版ffmpeg源码分析
最新版ffmpeg源码分析一:框架 (ffmpeg v0.9) 框架 最新版的ffmpeg中发现了一个新的东西:avconv,而且ffmpeg.c与avconv.c一个模样,一研究才发现是libav下 ...
- 【转】APP被苹果App Store拒绝的N个原因(持续补充)
作为iOS开发者,估计有很多都遇到过APP提交到App Store被拒,然后这些被拒的原因多种多样,今天dApps收集了常见的被拒的原因,以便更多开发者了解. 1.程序有重大bug,程序不能启动,或者 ...
- android 打包流程
.使用Android SDK提供的aapt.exe生成R.java类文件 .使用Android SDK提供的aidl.exe把.aidl转成.java文件(如果没有aidl,则跳过这一步) .使用JD ...
- Eclipse无法启动报An internal error occurred during: "reload maven project". java.lang.NullPointerException
由于没有正常关机导致eclipse无法将数据正常写入配置文件导致无法启动.报这样一个异常 An internal error occurred during: "reload maven p ...
- Xcode编译错误集锦
1.在将ios项目进行Archive打包时,Xcode提示以下错误: [BEROR]CodeSign error: Certificate identity ‘iPhone Distribution: ...
- HTML5 Canvas实战之刮奖效果
近年来由于移动设备对HTML5的较好支持,经常有活动用刮奖的效果,最近也在看H5方面的内容,就自己实现了一个,现分享出来跟大家交流. 1.效果 2.原理 原理很简单,就是在刮奖区添加两个canvas, ...