You can't specify target table 'ship_product_cat' for update in FROM clause

有时候咱们在编辑update时须要select做为条件,在mysql中有时会出现这样的错误:You can't specify target table for update in FROM clause错误的意思是说,不能先select出同一表中的某些值,再update这个表(在同一语句中)。mysql

例以下面这个sql:sql

UPDATE ship_product_cat SET is_parent = 0  WHERE id in(
SELECT id FROM ship_product_cat WHERE name in('Control','Propeller/Shaft')
);

错误信息:oracle

[SQL]UPDATE ship_product_cat SET is_parent = 0 WHERE id in(
SELECT id FROM ship_product_cat WHERE name in('Control','Propeller/Shaft'));
[Err] 1093 - You can't specify target table 'ship_product_cat' for update in FROM clause

 解决方法——换成下面的SQL就能够了 spa

UPDATE ship_product_cat SET is_parent = 0  WHERE id in(
SELECT a.id FROM
(SELECT id FROM ship_product_cat WHERE name in('Control','Propeller/Shaft'))a
);

 

也就是说将select出的结果再经过中间表select一遍,这样就规避了错误。code

 这个错误会出如今mysql中,但不会出如今oracle中。

相关文章
相关标签/搜索