Lucas
Lucas

Reputation: 2994

Is it possible to update the cell value only to the specific maximum value?

Is it possible to update the cell value only to the specific maximum value? Here is query:

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

I want to limit premium_photos (tinyint) value to max value of 4. Is it possible? For example if premium_photos current value is 2 and query is + 3, then after this query value will be 4.

Upvotes: 1

Views: 55

Answers (2)

Teneff
Teneff

Reputation: 32158

you can also use IF function

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

IF() function documentation

Upvotes: 1

Yahia
Yahia

Reputation: 70379

try

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

Upvotes: 2

Related Questions