是否可以仅将单元格值更新为特定的最大值?

时间:2012-02-03 08:57:09

标签: mysql sql

是否可以仅将单元格值更新为特定的最大值?这是查询:

UPDATE table_1 SET premium_photos = premium_photos + 2 WHERE number = '1234'

我想将premium_photos(tinyint)值限制为最大值4.是否可能?例如,如果premium_photos当前值为2且查询为+ 3,则此查询值后将为4.

2 个答案:

答案 0 :(得分:2)

UPDATE table_1 SET 
premium_photos = (CASE WHEN (premium_photos + 2) > 4 THEN 4 ELSE (premium_photos + 2) END) 
WHERE number = '1234'

答案 1 :(得分:1)

您也可以使用IF功能

UPDATE table_1 SET
   premium_photos = IF(premium_photos+2>4, 4, premium_photos+2)
WHERE number = '1234'

IF() function documentation