开发者

How can I rewrite this query so as to avoid the error: You can't specify target table for update in FROM clause

开发者 https://www.devze.com 2023-02-11 22:30 出处:网络
update websites set master = 2 where url = select url from websites where id = 12; Apparently mysql won\'t allow you t开发者_运维技巧o run a select query on table you\'re updating.Put it into a deri
update websites set master = 2 where url = select url from websites where id = 12;

Apparently mysql won't allow you t开发者_运维技巧o run a select query on table you're updating.


Put it into a derived table. This gets materialised into a temp table and gets around the restriction.

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


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

精彩评论

暂无评论...
验证码 换一张
取 消