Bobby
Bobby

Reputation: 87

Mysql Update Set Max Values

I would like to update mysql. Here is the query

UPDATE citizen_data SET energy=energy+?,hunger=hunger+?,thirsty=thirsty+?,mood=mood+? WHERE username=?

The problem is

Upvotes: 0

Views: 32

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522211

You may use the LEAST() scalar function here:

UPDATE citizen_data
SET energy = LEAST(energy + 20, 100)
-- other set criteria here...

Here we take the smaller of energy + 20 or 100. For energy values such that adding 20 would result in a value over 100, we update with 100, otherwise we update with that sum.

Upvotes: 1

Related Questions