限制 MySQL 中的数量达最大值?
为此,可以使用 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)
广告