当前位置:网站首页>You can't specify target table for update in from clause error in MySQL
You can't specify target table for update in from clause error in MySQL
2022-06-25 05:55:00 【Qingfeng junior】
One 、 analysis
mysql appear You can’t specify target table for update in FROM clause This mistake means that it can't be in the same sql In the sentence , First select Some values of the same table , And then again update This table
Two 、 The phenomenon
for example :message Table holds messages from multiple users
Create table
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 data
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');
Table structure and data are as follows :
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)
What makes a mistake SQL
Then execute to update the content of the first message of each user to 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
Because in the same sql In the sentence , First select Out message Minimum number of messages per user in the table id value , Then update message surface , So there will be ERROR 1093 (HY000): You can’t specify target table ‘message’ for update in FROM clause This mistake .
3、 ... and 、 resolvent
select The results are then passed through an intermediate table select One more time , You can avoid this mistake
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 );
perform
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)
Be careful , Only mysql There will be this problem ,mssql And oracle No such problem .
Four 、 explain
When a request is reading data , Other requests can also be read , But I can't write , Because once another thread writes data , This will cause the data read by the current thread to be not the latest , This is the phenomenon of non repeatable reading . This limitation can be circumvented by using the form of generated tables , because MySQL This table will only be treated as a temporary table .
Link to the original text :https://blog.csdn.net/fdipzone/article/details/52695371
边栏推荐
- How to add an external header file in vs?
- C language - minesweeping
- Learn the interface test, see it is very good, and make a note
- Use of arrays tool class
- 手机开户一般哪个证券公司好?手机开户是安全么?
- CSDN cerebral palsy bug has wasted nearly two hours of hard work
- MySQL operation JSON
- [kicad image] download and installation
- Guava immutable set
- SQL get current time
猜你喜欢
SAP Fiori tools and corresponding cli (command line interface)

HashSet implementation class

Folding mobile phones are expected to explode, or help Samsung compete with apple and Chinese mobile phones
SAP ui5 beginner tutorial 25 - using proxy server to solve the cross domain problem of SAP ui5 application accessing remote OData service trial version

MySQL uses the where condition to find strange results: solve
Some common errors and solutions of using SAP ui5 to consume OData services

Classic usage of the sumproduct function
Introduction to the main features of kyma when the cloud native application runs
SAP ui5 beginner tutorial No. 28 - Introduction to the integration test tool OPA for SAP ui5 applications

Array introduction plus example 01
随机推荐
CVPR2021-Semi-supervised Domain Adaptation based on Dual-level Domain Mixing for Semantic Segmentati
Common cluster deployment schemes in redis
Kubevela v1.2 release: the graphical operation console velaux you want is finally here!
MySQL tuning -- 02 -- slow query log
Word quickly makes multiple single-sided table labels, number plates, etc
What is flush software? Is it safe to open an account online?
Linus' speech recordings, which were lost in 1994, were made public
Folding mobile phones are expected to explode, or help Samsung compete with apple and Chinese mobile phones
A method of automatic continuation of previous tables in word table
Configuration file ui5 local in SAP ui5 tools Configuration points of yaml
SAP ui5 tutorial for beginners part XXVI - detailed steps for using OData service with mock server trial version
SAP ui5 beginner tutorial 25 - using proxy server to solve the cross domain problem of SAP ui5 application accessing remote OData service trial version
Depth of binary tree
Try with resource close resource flow
Day21 performance test process
JSON Library Tutorial from scratch (I): starting to learn and organize notes
D compile time reflection
ThreadLocal
Mirror image of binary tree
SAP ui5 beginner tutorial No. 28 - Introduction to the integration test tool OPA for SAP ui5 applications