如何减少MySQL中的值使其保持零以上?
您可以使用更新命令在MySQL中减少值。这样,您也可以将值限制为不小于0。
语法如下-
update yourTableName set yourColumnName = yourColumnName - 1 where yourColumnName > 0;
为避免该值降至零以下,可以使用yourColumnName>0。
为了理解上述语法,让我们创建一个表。查询创建表。
mysql> create table DecrementDemo −> ( −> DecrementValue int −> );
使用insert语句在表中插入一些记录。查询如下-
mysql> insert into DecrementDemo values(15),(14),(13),(12),(11),(10); Records: 6 Duplicates: 0 Warnings: 0
现在,您可以在select语句的帮助下显示表中的所有记录。查询如下-
mysql> select *from DecrementDemo;
以下是输出-
+----------------+ | DecrementValue | +----------------+ | 15 | | 14 | | 13 | | 12 | | 11 | | 10 | +----------------+ 6 rows in set (0.00 sec)
这是查询以减少表中的值-
mysql> update DecrementDemo −> set DecrementValue = DecrementValue - 1 where DecrementValue > 0; Rows matched: 6 Changed: 6 Warnings: 0
使用以下查询检查值是否递减-
mysql> select *from DecrementDemo;
以下是输出-
+----------------+ | DecrementValue | +----------------+ | 14 | | 13 | | 12 | | 11 | | 10 | | 9 | +----------------+ 6 rows in set (0.00 sec)
热门推荐
10 读书祝福语女生短句简短
11 感谢同事新婚祝福语简短
12 感谢教官文案祝福语简短
13 员工聚会蛋糕祝福语简短
14 元宵发客户祝福语简短
15 给团队信心祝福语简短
16 医美的祝福语大全简短
17 异国姻缘的祝福语简短
18 兄妹拍照祝福语简短的话