You can't specify target table 'student_ly' for update in FROM clause
时间: 2023-12-23 19:05:45 浏览: 82
这个错误通常是因为在 UPDATE 语句的 FROM 子句中使用了与要更新的目标表相同的表。MySQL 不允许直接在同一查询中更新和选择同一个表。
解决方案是将要更新的表和 FROM 子句中的表分开,可以使用子查询来实现。例如:
```
UPDATE student_ly
SET score = 90
WHERE id IN (
SELECT id
FROM student_ly
WHERE grade = 'A'
);
```
这个查询中,子查询先选择了要更新的记录的 ID,然后在 UPDATE 语句中使用这个子查询作为条件来更新分数。这样就可以避免在同一个查询中更新和选择同一个表的问题。
相关问题
You can t specify target table res_device_info for update in FROM clause
This error message usually occurs in SQL when you try to update a table that is also used in the FROM clause of the same query. To resolve this issue, you can use a subquery to update the table instead. Here's an example:
```sql
UPDATE res_device_info
SET column_name = new_value
WHERE device_id IN (
SELECT device_id
FROM res_device_info
WHERE condition
);
```
In this example, you can replace `column_name`, `new_value`, `condition` with the appropriate values for your scenario. Make sure to adjust the subquery's condition to match your requirements.
You can't specify target table 'student' for update in FROM clause
这个错误通常是在MySQL中进行多表更新时出现的。出现这个错误的原因是因为MySQL不允许在同一查询中更新和选择同一个表。解决此问题的一种方法是使用子查询或临时表来避免同时更新和选择同一个表。
以下是使用子查询的示例:
UPDATE student
SET name = 'John Doe'
WHERE id IN (SELECT id FROM (SELECT id FROM student) AS temp);
以下是使用临时表的示例:
CREATE TEMPORARY TABLE temp_student
SELECT id FROM student;
UPDATE student
SET name = 'John Doe'
WHERE id IN (SELECT id FROM temp_student);
DROP TABLE temp_student;
阅读全文