如何重写此查询以避免错误:您不能在 FROM 子句中指定要更新的目标表 [英] How can I rewrite this query so as to avoid the error: You can't specify target table for update in FROM clause

查看:43
本文介绍了如何重写此查询以避免错误:您不能在 FROM 子句中指定要更新的目标表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

update websites set master = 2 where url = select url from websites where id = 12;

显然 mysql 不允许您对正在更新的表运行选择查询.

Apparently mysql won't allow you to run a select query on table you're updating.

推荐答案

放入派生表中.这得到实体化到临时表中并绕过限制.

update websites
set    master = 2
where  url in (select url
               from   (select url
                       from   websites
                       where  id = 12) t);  

这篇关于如何重写此查询以避免错误:您不能在 FROM 子句中指定要更新的目标表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆