MySql报错 1093 - You can’t specify target table xxx for update in FROM clause

Written in the front : I am "Sailing to the Sea" . This nickname comes from my name and the name of my girlfriend. I love technology, open source, and programming. 技术是开源的、知识是共享的.

This blog is a little summary and record of your own learning. If you are interested in Java and algorithms , you can follow my dynamics and we will learn together.

用知识改变命运,让我们的家人过上更好的生活.

MySQL encountered such an error during development 1093 - You can’t specify target table 'person' for update in FROM clause. Below, I will take a topic of Likou as an example for analysis.

1. Source of the subject

+----+------------------+
| Id | Email            |
+----+------------------+
| 1  | john@example.com |
| 2  | bob@example.com  |
| 3  | john@example.com |
+----+------------------+

For example, after running your query, the Person table above should return the following rows:

+----+------------------+
| Id | Email            |
+----+------------------+
| 1  | john@example.com |
| 2  | bob@example.com  |
+----+------------------+

Tips :

  • After executing the SQL, the output is the entire Person table.
  • Use the delete statement.

Source: LeetCode
Link: https://leetcode-cn.com/problems/delete-duplicate-emails
Copyright is owned by LeetCode . For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

Two, error analysis

Insert picture description here

This is because MySQL does not allow you to update and delete a table at the same time, that is, you cannot modify or delete based on the result of a query

Three, the solution

使用中间表将查询结果再次SELECT即可避免这个报错。

Insert picture description here


If you think the article is a bit helpful to you, please search for " 程序猿编程" on WeChat to read it for the first time!

On the road of moving bricks, we work hard together and knock out our future with both hands!

Insert picture description here


Due to the limited level, this blog will inevitably have inadequacies. I hope you guys can give me your advice!

Guess you like

Origin blog.csdn.net/weixin_43570367/article/details/108733195