You can't specify target table 'employee' for update in FROM clause
时间: 2023-12-25 22:03:55 浏览: 201
mysql中You can’t specify target table for update in FROM clause错误解决方法
这个错误通常出现在MySQL中,是因为在更新语句中使用了子查询,而子查询引用了需要更新的表,导致MySQL无法同时读写同一张表。解决这个问题的方法是使用临时表或者内联表来避免同时读写同一张表。
例如,下面是一个错误的更新语句:
```
UPDATE employee SET salary = salary * 1.1 WHERE employee_id IN (
SELECT employee_id FROM employee WHERE department = 'sales'
);
```
可以使用临时表来避免这个错误:
```
CREATE TEMPORARY TABLE tmp_employee SELECT employee_id FROM employee WHERE department = 'sales';
UPDATE employee SET salary = salary * 1.1 WHERE employee_id IN (
SELECT employee_id FROM tmp_employee
);
DROP TEMPORARY TABLE tmp_employee;
```
或者使用内联表:
```
UPDATE employee
JOIN (
SELECT employee_id FROM employee WHERE department = 'sales'
) AS tmp_employee ON employee.employee_id = tmp_employee.employee_id
SET salary = salary * 1.1;
```
阅读全文