这条sql语句错在什么地方?

DELETE from li_make_code  where user_id in 
(    
    select user_id from li_make_code a 
        where 
            not exists (select 1 from li_user b  where b.id=a.user_id)
) ;
[Err] 1093 - You can't specify target table 'li_make_code' for update in FROM clause

这个语句应该怎么改合适

阅读 2.7k
4 个回答

你是想把user_idli_user表中找不到的给删了吧,为什么不直接用:

delete from li_make_code where user_id not in 
    (select distinct id from li_user)
新手上路,请多包涵

你子查询和删除操作的是同一个表,不能同时查询和更新同一个表。
更改为join方式即可。

DELETE from li_make_code where user_id in ( SELECT user_id from
(select user_id from li_make_code a where not exists(select 1 from li_user b where b.id=a.user_id)) bb)

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题