update restaurant
set AverageCost=null
where AverageCost=-1
_____________________________________________________________________________________
update restarant
set AverageCost=null
where dbID in (select dbID
from restaurant
where AverageCost=-1)
以上都是错的。。。。
1. 执行时会错误,报:
You are using safe update mode and you tried to update a table without a WHERE clause that
uses a KEY column.”
原因是在safe mode下,要强制安全点,update只能跟where了,要取消这个限制,可以:
SET SQL_SAFE_UPDATES=0;
2. 执行时会错误,报:
You can't specify target table 'restaurant' for update in FROM clause
mysql中不能先select出同一表中的某些值,再update这个表(在同一语句中)
正确update方法:
1. create table tmp as select dbID as ID from restaurant where AverageCost=-1;
update restaurant set AverageCost=null where dbID in (select ID from tmp);
drop table tmp;
先生成一个临时表,在update,但是又遇到了问题,它太慢太慢太慢了,可能是因为有表连接。。。换第二种方法
2. update restaurant
inner join tmp on dbID=ID
set AverageCost=null
转载请注明:谷谷点程序 » Mysql更新语句update问题