要获取最大值,请使用以下任何语法-
select max(yourColumnName) from yourTableName; OR select *from yourTableName order by yourColumnName desc limit 1;
让我们首先创建一个表-
mysql> create table DemoTable ( Value int );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values(45); mysql> insert into DemoTable values(87); mysql> insert into DemoTable values(56); mysql> insert into DemoTable values(77); mysql> insert into DemoTable values(85);
使用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)