从 MySQL 数字列中提取最大值两种方法
要提取最大值,请使用下面给出的任何一种语法 -
select max(yourColumnName) from yourTableName; OR select *from yourTableName order by yourColumnName desc limit 1;
让我们首先创建一个表 -
mysql> create table DemoTable ( Value int ); Query OK, 0 rows affected (0.84 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable values(45); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(87); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(56); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(77); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(85); Query OK, 1 row affected (0.17 sec)
使用 select 语句从表中显示所有记录 -
mysql> select *from DemoTable;
这将产生以下输出 -
+-------+ | Value | +-------+ | 45 | | 87 | | 56 | | 77 | | 85 | +-------+ 5 rows in set (0.00 sec)
现在,让我们从上表中获取最大值 -
mysql> select max(Value) from DemoTable;
这将产生以下输出 -
+------------+ | max(Value) | +------------+ | 87 | +------------+ 1 row in set (0.00 sec)
现在,让我们看看获取最大值的另一种方法 -
mysql> select *from DemoTable order by Value desc limit 1;
这将产生以下输出 -
+-------+ | Value | +-------+ | 87 | +-------+ 1 row in set (0.00 sec)
广告