mysql只有在新值低于现有值时才更新字段

时间:2011-12-04 14:42:17

标签: mysql

我想仅在新字段较低时才更新字段的值。我尝试了这样的min,没有成功

UPDATE `editions` SET `editions`.`year`  = MIN(`editions`.`year` , '2005')  WHERE `editions`.`id` = 1;

我收到1064错误。如何在不先检索值的情况下更新值?

2 个答案:

答案 0 :(得分:4)

UPDATE `editions` 
SET `editions`.`year` = 
LEAST(`editions`.`year` , '2005')  WHERE `editions`.`id` = 1;

答案 1 :(得分:2)

您是否尝试过这样做:

update editions set year = 2005 where id = 1 and year > 2005;

(即将选择限制为您想要更新的行)