MySQL中You can't specify target table for update in FROM clause错误的意思是说,不能先select出同一表中的某些值,再update这个表(在同一语句中)。 例如下面这个sql:
UPDATE tb_star_incr SET `status`='1' WHERE star_id in (SELECT DISTINCT star_id FROM tb_star_incr WHERE `status`='0' and p_type!="组合类" AND CAST(score AS signed)>=10);
运行时会报You can't specify target table for update in FROM clause的错误,改写成下面就行了:
UPDATE tb_star_incr SET `status`='1' WHERE star_id in (SELECT * FROM (SELECT DISTINCT star_id FROM tb_star_incr WHERE `status`='0' and p_type!="组合类" AND CAST(score AS signed)>=10) AS t1);
也就是说将select出的结果再通过中间表select一遍,这样就规避了错误。
You can't specify target table for update in FROM clause含义:不能在同一表中查询的数据作为同一表的更新数据