在 MySQL 中查找大于某个数字的下一个最小数字?
mysqlmysqli database更新于 2025/12/24 2:20:17
为此,使用聚合函数 MIN() 和 WHERE 子句。让我们首先创建一个表 −
mysql> create table DemoTable ( Number int ); Query OK, 0 rows affected (0.54 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable values(11); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(9); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(12); Query OK, 1 row affected (0.11 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable;
这将产生以下输出 −
+--------+ | Number | +--------+ | 11 | | 9 | | 10 | | 12 | +--------+ 4 rows in set (0.00 sec)
以下是在 MySQL 中查找大于某个数字的下一个最小数字的查询 −
mysql> select min(Number) from DemoTable where Number > 10;
这将产生以下输出 −
+-------------+ | min(Number) | +-------------+ | 11 | +-------------+ 1 row in set (0.00 sec)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

