阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

MySQL 出现You can’t specify target table for update in FROM clause错误解决方法

269次阅读
没有评论

共计 2633 个字符,预计需要花费 7 分钟才能阅读完成。

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 都没有这个问题。

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-02/140209.htm

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计2633字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中