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

  • 59
    点赞
  • 86
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
引用\[1\]:这是一个正确的UPDATE语句示例,它将t_user表中id为'U0123456789'的记录的user_name字段更新为t_user表中任意一条记录的user_name字段的值,并将update_time字段更新为当前时间。\[1\] 引用\[2\]:这是一个错误UPDATE语句示例,它试图在子查询中更新目标表t_user的字段user_name,这是不允许的。\[2\] 引用\[3\]:这是一个引发报错的SQL语句示例,报错信息为"You can't specify target table 'email' for update in FROM clause"。这个错误通常是由于在UPDATE语句中的子查询中引用了目标表本身导致的。解决这个问题的方法是使用临时表或者将子查询的结果存储到变量中,然后再进行更新操作。\[3\] 问题:You can't specify target table 'email' for update in FROM clause 回答:这个错误通常是由于在UPDATE语句中的子查询中引用了目标表本身导致的。解决这个问题的方法是使用临时表或者将子查询的结果存储到变量中,然后再进行更新操作。 #### 引用[.reference_title] - *1* *2* [You can‘t specify target table for update in FROM clause](https://blog.csdn.net/aayygg1234/article/details/121753911)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [MySql:You can not specify target table for update in FROM clause错误](https://blog.csdn.net/qq_42213910/article/details/106918293)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值