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都没有这个问题。
例子:
CREATE TABLE `ppos_tmp3` (
`id` int(11) NOT NULL AUTO_INCREMENT ,
`business_id` int(11) NOT NULL ,
`card_num` bigint(18) NULL DEFAULT NULL ,
`p_card_num` char(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
PRIMARY KEY (`id`)
)
; insert into ppos_tmp3 select id,business_id,card_num,p_card_num from ppos_member_card_src a
where (a.business_id,a.p_card_num) in (select business_id,p_card_num from ppos_member_card_src group by business_id,p_card_num having count(*) > 1)
and id not in (select min(id) from ppos_member_card_src group by business_id,p_card_num having count(*)>1) and activate_stadium_id=100004; delete from ppos_member_card_src where id in (select id from ppos_tmp3) and activate_stadium_id=100004;
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--mysql中You can’t specify target table for update in FROM clause错误解决方法
参考:http://www.jb51.net/article/60926.htm mysql中You can't specify target table for update in FROM cla ...
- 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错误:1093-You can’t specify target table for update in FROM clause的解决方法
update语句中包含的子查询的表和update的表为同一张表时,报错:1093-You can’t specify target table for update in FROM clause my ...
- 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这个表( ...
随机推荐
- R12.1.3 & R12.2.X 注册客户化应用
R12.2 官方出了运行补丁脚本就可以自动创建客户化应用,所以也把此补丁应用在 R12.1.3上验证一下其可行性,做出以下实验. 1.创建客户化应用账号CUX --login user system ...
- nginx多站路由配置tomcat
server { listen 80; server_name 1.goal.cn; index index index.html index.htm index.jsp; root /www/ser ...
- 【ZH奶酪】如何用sklearn计算中文文本TF-IDF?
1. 什么是TF-IDF tf-idf(英语:term frequency–inverse document frequency)是一种用于信息检索与文本挖掘的常用加权技术.tf-idf是一种统计方法 ...
- oracle 11g 安装及netca,dbca乱码之解决
在中文Linux下安装Oracle 11g,运行runInstaller后默认会出现乱码,解决办法如下: 1.准备字体zysong.ttf,点击下载,解压下载到的fallback 2.使用归档管理器打 ...
- Jexus 网站服务器和 ASP.NET 跨平台开发
微软的跨平台战略 微软在过去的一年多中时间中发生了令整个 IT 行业感到惊叹的变化.这一切始于 Ballmer 的退位和 Nadella 的决心,更始于早已在微软各个基层部门蠢蠢欲动的二次创业. 以开 ...
- python实现模拟登录
本文主要用python实现了对网站的模拟登录.通过自己构造post数据来用Python实现登录过程. 当你要模拟登录一个网站时,首先要搞清楚网站的登录处理细节(发了什么样的数据,给谁发等...). ...
- 如何在页面中获取到ModelAndView绑定的值
以下内容转自:https://blog.csdn.net/qq_16071145/article/details/51341052 springMVC中通过ModelAndView进行后台与页面的数据 ...
- NDK配置
NDK 配置 Android SDK中下载NDK, LLDB Android.mk 和 Application.mk 简单来说 Android.mk 用来描述需要生成哪些模块的 .so 文件 Appl ...
- 11G新特性 -- variable size extents
AU是asm磁盘分配的基本单元.在oracle10g中,一个AU对应一个extent(这会增加对内存的使用),因为一个大的数据库如果含有大量的默认大小的AU,会导致数据库的share pool的大量使 ...
- 8个非常个性化的CSS3单/复选框
单选框和复选框在网页表单中应用十分广泛,但是浏览器默认自带的单选框和复选框样式不仅不统一,而且大多都比较简单丑陋.本文给大家介绍了一些基于CSS3的个性化单选框和复选框,一些选中动画是基于jQuery ...