在 MySQL 中将数字限制为最大值?
mysqlmysqli database
为此,您可以使用 LEAST()。以下是语法 −
select least(yourColumnName,yourMaxValue) from yourTableName;
首先我们创建一个表 −
mysql> create table DemoTable -> ( -> Number int -> ); Query OK, 0 rows affected (0.64 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable values(50); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(40); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(30); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(201); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(60); Query OK, 1 row affected (0.17 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable;
输出
+--------+ | Number | +--------+ | 50 | | 40 | | 30 | | 201 | | 60 | +--------+ 5 rows in set (0.00 sec)
这是在 MySQL 中将数字限制为最大值的查询。我们将所有数字的最大值限制为 40 −
mysql> select least(Number,40) from DemoTable;
输出
+------------------+ | least(Number,40) | +------------------+ | 40 | | 40 | | 30 | | 40 | | 40 | +------------------+ 5 rows in set (0.02 sec)