3

I have a field in my database that I don't want to exceed 100. Is there something in a query I can add to make sure it doesn't?

I usually end up doing this:

UPDATE table SET field = field + $x UPDATE table SET field = 100 WHERE field > 100 

Does anything like this exist?

2 Answers 2

4
UPDATE table SET field = CASE WHEN field + $x <= 100 THEN field + $x ELSE 100 END ; 

or:

UPDATE table SET field = LEAST(field + $x, 100) ; 
Sign up to request clarification or add additional context in comments.

Comments

0

what about a trigger? something like

CREATE TRIGGER tg_limit BEFORE UPDATE ON tbl FOR EACH ROW BEGIN IF NEW.fld > 100 THEN SET NEW.fld = 100; END IF; END 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.