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错误的解决方法的更多相关文章

  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错误的意思是说,不能先select出同一表中的某些值,再update这个表( ...

  2. 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 ...

  3. 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同一个表的某些值 ...

  4. 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 ...

  5. 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 ...

  6. 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这个表( ...

  7. 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这个表( ...

  8. 解决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这个表( ...

  9. 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这个表( ...

随机推荐

  1. xxd十六进制编辑器的安装

    一.背景:在vi中使用命令:%!xxd无法进行十六进制编辑,为缺少xxd命令所致 二.yum直接安装xxd无法成功[root@ELK ~]# yum install xxd已加载插件:fastestm ...

  2. 【Mybatis】mybatis开启Log4j日志、增删改查操作

    Mybatis日志(最常用的Log4j) 官方网站http://www.mybatis.org/mybatis-3/zh/logging.html 1.在src目录下创建一个log4j.propert ...

  3. JavaSE (六)面向对象 -- 类的结构

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 目录 一.属性(变量) 1.变量的分类: 二.方法 1.例子: 2.格式: 3.方法的说明: 4.ret ...

  4. Java实现 LeetCode 781 森林中的兔子(分析题)

    781. 森林中的兔子 森林中,每个兔子都有颜色.其中一些兔子(可能是全部)告诉你还有多少其他的兔子和自己有相同的颜色.我们将这些回答放在 answers 数组里. 返回森林中兔子的最少数量. 示例: ...

  5. Java实现 蓝桥杯VIP 算法提高 盾神与砝码称重

    算法提高 盾神与砝码称重 时间限制:1.0s 内存限制:256.0MB 提交此题 查看参考代码 问题描述 有一天,他在宿舍里无意中发现了一个天平!这个天平很奇怪,有n个完好的砝码,但是没有游码.盾神为 ...

  6. java实现祖冲之割圆法

    祖冲之割圆法 南北朝时,我国数学家祖冲之首先把圆周率值 计算到小数点后六位,比欧洲早了1100年!他采 用的是称为"割圆法"的算法,实际上已经蕴含 着现代微积分的思想. 如图[1. ...

  7. java实现第三届蓝桥杯古代赌局

    古代赌局 [编程题](满分23分) 俗话说:十赌九输.因为大多数赌局的背后都藏有阴谋.不过也不尽然,有些赌局背后藏有的是:"阳谋". 有一种赌局是这样的:桌子上放六个匣子,编号是1 ...

  8. java实现第五届蓝桥杯武功秘籍

    武功秘籍 小明到X山洞探险,捡到一本有破损的武功秘籍(2000多页!当然是伪造的).他注意到:书的第10页和第11页在同一张纸上,但第11页和第12页不在同一张纸上. 小明只想练习该书的第81页到第9 ...

  9. Java学习的一般过程

    伴随着科学技术的不断发展,世界开始走向信息化.网络化.大数据化.自然而然,计算机专业变得十分热门.尽管如此,计算机专业人才对社会来说仍然是供不应求,当然,这里指的是高层次技术人才.因此,对于我们这些占 ...

  10. 温故知新-java的I/O模型-BIO&NIO&AIO

    文章目录 摘要 传统的BIO编程 伪异步I/O编程 NIO编程 AIO编程 几种IO模型的对比 netty 参考 你的鼓励也是我创作的动力 Posted by 微博@Yangsc_o 原创文章,版权声 ...