当前位置:网站首页>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
边栏推荐
- MySQL uses the where condition to find strange results: solve
- Semantic segmentation cvpr2019-advance: advantageous enterprise minimization for domain adaptation in semantic segmentation
- Guava immutable set
- Oracle SQL statement operand: rounding, rounding, differentiation and formatting
- Trouble of setting table property to null
- Day18 (set, generic, hash table, tree, stack and queue, graph, array and linked list)
- An example of recursion, the full permutation problem of 1~n
- Introduction to sap ui5 tools
- [pan Wai 1] Huawei computer test
- ERDAS 9.2 installation tutorial
猜你喜欢
SAP ui5 application development tutorial XXIX - Introduction to routing and navigation functions of SAP ui5 trial version

Laravel8 fill data
Wind farm visualization: wind farm data

MySQL operation JSON
Websocket in the promotion of vegetable farmers

C language -- Sanzi chess

Semantic segmentation cvpr2019-advance: advantageous enterprise minimization for domain adaptation in semantic segmentation

Jenkins installation and configuration

Voxel based and second network learning

No one reads the series. Source code analysis of copyonwritearraylist
随机推荐
Day19 (variable parameter, enhanced for loop traversal, generic wildcard <? >, TreeSet, linkedhashset, nested traversal of sets, set set, static import,)
[interview with a large factory] meituan had two meetings. Was there a surprise in the end?
Soft exam information system project manager_ Management Science (Operations Research) 2--- senior information system project manager of soft test 034
Folding mobile phones are expected to explode, or help Samsung compete with apple and Chinese mobile phones
Monkey test of APP automation
[JS basic review] scope, this, closure
Use of pytorch tensorboard
Pytorch- daily learning notes of some small functions involving training
MySQL transaction learning notes (I) first encounter
Technology inventory: Technology Evolution and Future Trend Outlook of cloud native Middleware
Technology inventory: past, present and future of Message Oriented Middleware
[QT] for multithreaded programs, do not use the printf() function to print out
Double recursion in deep analysis merge sort
The simplest way to tell you is to hash and not hash
3.2.3 use tcpdump to observe TCP header information (supplement common knowledge of TCP protocol)
Soft exam information system project manager_ Information system security management - Senior Information System Project Manager of soft test 026
Interview experience - list of questions
Only these four instructions are required to operate SQL data
05 virtual machine stack
Get the first letter of Chinese phonetic alphabet in Excel and capitalize it