You can't specify target table 'PP' for update in FROM clause
时间: 2023-09-24 10:09:52 浏览: 214
This error message is usually encountered when trying to update a table that is being used as a subquery in the same UPDATE statement. The MySQL database does not allow this type of operation because it can create a conflict between the original data and the updated data.
To resolve this error, you can use a temporary table to store the results of the subquery and then use that table to update the target table. Another option is to rewrite the UPDATE statement using a JOIN clause instead of a subquery, which can avoid the conflict issue.
Here's an example of using a temporary table to update the target table:
```
CREATE TEMPORARY TABLE tmp_table AS
SELECT * FROM PP WHERE some_condition;
UPDATE PP
SET column1 = value1, column2 = value2
WHERE id IN (SELECT id FROM tmp_table);
```
And here's an example of using a JOIN clause to update the target table:
```
UPDATE PP
JOIN (SELECT id FROM PP WHERE some_condition) AS tmp_table
ON PP.id = tmp_table.id
SET column1 = value1, column2 = value2;
```
Both of these approaches can help you avoid the "Can't specify target table for update in FROM clause" error.
阅读全文