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同一个表的某些值,然后再update这个表。
例如:message表保存了多个用户的消息
创建表
CREATE TABLE `message` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`uid` int(10) unsigned NOT NULL,
`content` varchar(255) NOT NULL,
`addtime` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `uid` (`uid`),
KEY `addtime` (`addtime`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
插入数据
insert into message(uid,content,addtime) values
(1,'content1','2016-09-26 00:00:01'),
(2,'content2','2016-09-26 00:00:02'),
(3,'content3','2016-09-26 00:00:03'),
(1,'content4','2016-09-26 00:00:04'),
(3,'content5','2016-09-26 00:00:05'),
(2,'content6','2016-09-26 00:00:06'),
(2,'content7','2016-09-26 00:00:07'),
(4,'content8','2016-09-26 00:00:08'),
(4,'content9','2016-09-26 00:00:09'),
(1,'content10','2016-09-26 00:00:10');
表结构及数据如下:
mysql> select * from message;
+----+-----+-----------+---------------------+
| id | uid | content | addtime |
+----+-----+-----------+---------------------+
| 1 | 1 | content1 | 2016-09-26 00:00:01 |
| 2 | 2 | content2 | 2016-09-26 00:00:02 |
| 3 | 3 | content3 | 2016-09-26 00:00:03 |
| 4 | 1 | content4 | 2016-09-26 00:00:04 |
| 5 | 3 | content5 | 2016-09-26 00:00:05 |
| 6 | 2 | content6 | 2016-09-26 00:00:06 |
| 7 | 2 | content7 | 2016-09-26 00:00:07 |
| 8 | 4 | content8 | 2016-09-26 00:00:08 |
| 9 | 4 | content9 | 2016-09-26 00:00:09 |
| 10 | 1 | content10 | 2016-09-26 00:00:10 |
+----+-----+-----------+---------------------+
10 rows in set (0.00 sec)
然后执行将每个用户第一条消息的内容更新为Hello World
mysql> update message set content='Hello World' where id in(select min(id) from message group by uid);
ERROR 1093 (HY000): You can't specify target table 'message' for update in FROM clause
因为在同一个sql语句中,先select出message表中每个用户消息的最小id值,然后再更新message表,因此会出现 ERROR 1093 (HY000): You can’t specify target table ‘message’ for update in FROM clause 这个错误。
解决方法:select的结果再通过一个中间表select多一次,就可以避免这个错误
update message set content='Hello World' where id in( select min_id from ( select min(id) as min_id from message group by uid) as a );
mysql> update message set content='Hello World' where id in( select min_id from ( select min(id) as min_id from message group by uid) as a );
Query OK, 4 rows affected (0.01 sec)
Rows matched: 4 Changed: 4 Warnings: 0 mysql> select * from message;
+----+-----+-------------+---------------------+
| id | uid | content | addtime |
+----+-----+-------------+---------------------+
| 1 | 1 | Hello World | 2016-09-26 00:00:01 |
| 2 | 2 | Hello World | 2016-09-26 00:00:02 |
| 3 | 3 | Hello World | 2016-09-26 00:00:03 |
| 4 | 1 | content4 | 2016-09-26 00:00:04 |
| 5 | 3 | content5 | 2016-09-26 00:00:05 |
| 6 | 2 | content6 | 2016-09-26 00:00:06 |
| 7 | 2 | content7 | 2016-09-26 00:00:07 |
| 8 | 4 | Hello World | 2016-09-26 00:00:08 |
| 9 | 4 | content9 | 2016-09-26 00:00:09 |
| 10 | 1 | content10 | 2016-09-26 00:00:10 |
+----+-----+-------------+---------------------+
10 rows in set (0.00 sec)
注意,只有mysql会有这个问题,mssql与oracle都没有这个问题。
mysql 出现You can't specify target table for update in FROM clause错误的解决方法的更多相关文章
- 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错误的意思是说,不能先select出同一表中的某些值,再update这个表(在同一语句中)。
将select出的结果再通过中间表select一遍,这样就规避了错误.注意,这个问题只出现于mysql,mssql和oracle不会出现此问题. mysql中You can't specify tar ...
- 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 for update in FROM clause错误
原SQL delete from DEP_SYSTEM_PORTLET_SETTINGS where ID in ( select ID from DEP_SYSTEM_PORTLET_SETTING ...
- MySQL之You can't specify target table for update in FROM clause解决办法
这篇文章主要介绍了mysql中You can't specify target table for update in FROM clause错误解决方法,需要的朋友可以参考下 MySQL中You c ...
- 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 error: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中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这个表( ...
随机推荐
- 几个值得学习的Java博客
谈面试时从写一个单例开始究竟能问多深及终极解决方案 提到了<深入理解java虚拟器><java并发编程实践> 美团点评的技术博客:MySQL索引原理及慢查询优化 https: ...
- 关于CAS单点登录配置文件误修改导致的JSP报错问题
weblogic环境下搭建CXF环境时,为了避免CAS拦截webservice服务URL所以设置CAS为不拦截,无修改了CAS配置的其它地方导致工程启动正常,一旦访问就会报JSP的错误. [Servl ...
- python3程序开发指南——第1章 笔记
python文件的扩展名为.py ,但是python GUI程序的扩展名为.pyw 在python中,注释以#开始,作用范围为该行 IDLE提供了三个关键功能:输入python表达式与代码,并在pyt ...
- JavaScript 引用数据类型
目录 1. 问题描述 2. 原因分析 3. React 中的引用数据类型 4. 业务场景 5. 参考资料 1. 问题描述 今天在写一个代码题时候, 有一个BUG 导致自己停滞好久, 该BUG 可以描述 ...
- AddressBook/AddressBookUI
概述 在iOS中,有2个框架可以访问用户的通讯录.从iOS6开始,需要得到用户的授权才能访问通讯录,因此在使用之前,需要检查用户是否已经授权ABAddressBookGetAuthorizationS ...
- python(3.x)自动化全栈开发100天集训计划(跟上进度,到一个新高度)——day1
Day1 目录: Python介绍 * 了解Python的特点.发展史 * 介绍Python广泛的应用领域和前景 第一个Python程序 * 掌握Python代码的2种执行方式 变量 ...
- (一)用less+gulp+requireJs 搭建项目(了解less)
项目完结 做个总结: 公司网站重构,整站都需要重写,终于有机会接触下 less,gulp和requireJs,因为以前的工作就是写几个活动页,并没有机会用这些工具,废话不多说,先看下完成后的项目目录: ...
- Java实现 蓝桥杯VIP 算法提高 分分钟的碎碎念
算法提高 分分钟的碎碎念 时间限制:1.0s 内存限制:256.0MB 问题描述 以前有个孩子,他分分钟都在碎碎念.不过,他的念头之间是有因果关系的.他会在本子里记录每一个念头,并用箭头画出这个念头的 ...
- 第四届蓝桥杯JavaB组省赛真题
解题代码部分来自网友,如果有不对的地方,欢迎各位大佬评论 题目1.世纪末星期 题目描述 曾有邪教称1999年12月31日是世界末日.当然该谣言已经不攻自破. 还有人称今后的某个世纪末的12月31日,如 ...
- TZOJ 复习时间
描述 为了能过个好年,xhd开始复习了,于是每天晚上背着书往教室跑.为了追求更高的效率,xhd要根据难度值来选择合适的课程进行复习,复习后一门课的效率为前一门课之间的难度差的平方,而复习第一门课的效率 ...