Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set minimum value of MySQL field - possible?

Quick newbie MySQL question. What would be the simplest way to ensure that the minimum value of a given field is 0?

Basically, we have a script that runs automatically and subtracts an integer value from the value of a field every 15 minutes--but we want any entry that gets to 0 to stay at 0 and not go negative.

This could be simply done with a loop in PHP but I'm trying to limit calls to the database...

Is there a simple way to either make the minimum value for a field 0 or make it so any negative value put in that field automatically becomes 0?

Thanks!

like image 389
user405056 Avatar asked Feb 26 '23 12:02

user405056


1 Answers

Have you tried a simple conditional "WHERE" or is the problem more complex than that?

UPDATE mytable SET mycolumn=mycolumn-1 WHERE mycolumn>0
like image 158
treeface Avatar answered Mar 07 '23 18:03

treeface