是否可以仅将单元格值更新为特定的最大值?这是查询:
UPDATE table_1 SET premium_photos = premium_photos + 2 WHERE number = '1234'
我想将premium_photos
(tinyint)值限制为最大值4.是否可能?例如,如果premium_photos
当前值为2且查询为+ 3,则此查询值后将为4.
答案 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'