error:You can't specify target table 'test_user_table' for update in FROM clause
原因:您不能在FROM子句中指定目标表“ test_user_table”进行更新,就是说查出来的id集合不能直接作为再去更新这个表的条件
解决办法:将内层查询出的结果赋给一个变量集合,外层查询用这个自定义变量集合作为条件,即不会出错。
-- 错误sql
UPDATE test_user_table
SET group_id = 99
WHERE
id IN (
SELECT
u.id
FROM
test_user_resource_table ursc,
test_user_table u
WHERE
ursc.type_ = "HOUSE"
AND ursc.rsc_code = 'NNTDXXX001'
AND ursc.userid = u.id
AND u.user_type = "NORMAL"
)
-- 正确sql
UPDATE test_user_table
SET group_id = 99
WHERE
id IN (
SELECT a.id FROM
(
SELECT
u.id
FROM
test_user_resource_table ursc,
test_user_table u
WHERE
ursc.type_ = "HOUSE"
AND ursc.rsc_code = 'NNTDXXX001'
AND ursc.userid = u.id
AND u.user_type = "NORMAL"
) AS a
)