2014-08-28 57 views
-2

我用下面的查询来更新MYSQL行:无法使用子查询

update city 
set CountryCode = (select CountryCode from city where id = 1)  
where id =4081; 

和我得到了以下错误:

Error Code: 1093. You can't specify target table 'city' for update in FROM clause

,但我不知道这是为什么不允许我这样做?

+1

-1花费0时google搜索解决方案。 – 2014-08-28 13:36:50

+1

可能重复的[Mysql错误1093 - 无法指定目标表更新FROM子句](http://stackoverflow.com/questions/45494/mysql-error-1093-cant-specify-target-table-for-更新功能于从子句) – 2014-08-28 13:37:00

回答

1

你可以使用一个小技巧是这样的,使得使用子子查询:

UPDATE 
    city 
SET 
    CountryCode = (SELECT * FROM (SELECT CountryCode FROM city where ID=1) s) 
WHERE 
    id=4081;