版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sunyuhua_keyboard/article/details/86235623
今天遇到一个情况,使用新查询出来的值作为键值更新其他的表更新不了,报
[Err] 1093 - You can't specify target table 'cmpt_main' for update in FROM clause
我的mysql更新语句为:
UPDATE cmpt_main SET cmpt_status=3 where cmpt_id in (
select cmptId from cmpt_main as a LEFT JOIN time_checkinrecord as b on a.cmpt_id = b.cmptId where cmpt_status is NULL and org_id like '1-3%' and a.cmpt_name not like '%测%' GROUP BY cmptId HAVING count(cmptId)> 200
)
You can’t specify target table ‘cmpt_main’ for update in FROM clause,报错的意思是:不能使用从一个表查询出来的语句更新这个表。
解决思路:
使用其他的临时表更新
UPDATE cmpt_main SET cmpt_status=3 where cmpt_id in (
SELECT cmptId from (
select cmptId from cmpt_main as a LEFT JOIN time_checkinrecord as b on a.cmpt_id = b.cmptId where cmpt_status is NULL and org_id like '1-3%' and a.cmpt_name not like '%测%' GROUP BY cmptId HAVING count(cmptId)> 200
) as T
)
这样就能更新成功。
sql执行顺序是: