Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有这个查询
UPDATE table SET this = this-20 WHERE this >= 0
我不希望this字段低于 0。我可以在此查询中添加一些附加内容以确保这一点,而无需运行另一个查询?
this
例如,如果这是 18,那么它将被设置为 -2
(使用 MYSQL)
您可以使用Greatest功能:
UPDATE table SET this = greatest(0,this-20) WHERE this >= 0
考虑到this - 20和0 ,基本上将 this 的值设置为更大的值。
将您的条件更改为WHERE (this - 20) >= 0。并创建一个检查约束:ALTER TABLE [table] ADD CONSTRAINT ThisGreaterThanZero CHECK (this >= 0).
WHERE (this - 20) >= 0
ALTER TABLE [table] ADD CONSTRAINT ThisGreaterThanZero CHECK (this >= 0)