您可以使用ORDER BY子句或聚合函数MAX()
来选择最大值。
以下是语法-
select yourColumnName from yourTableName order by yourColumnName desc limit 0,1;
让我们首先创建一个表-
mysql> create table DemoTable ( Number int );
使用插入命令在表中插入记录-
mysql> insert into DemoTable values(790); mysql> insert into DemoTable values(746); mysql> insert into DemoTable values(480); mysql> insert into DemoTable values(880); mysql> insert into DemoTable values(879);
使用select语句显示表中的所有记录-
mysql> select * from DemoTable;
这将产生以下输出-
+--------+ | Number | +--------+ | 790 | | 746 | | 480 | | 880 | | 879 | +--------+ 5 rows in set (0.00 sec)
以下是查询以选择MySQL中列的最大值的查询-
mysql> select Number from DemoTable order by Number desc limit 0,1;
这将产生以下输出-
+--------+ | Number | +--------+ | 880 | +--------+ 1 row in set (0.00 sec)
MAX()
您也可以MAX()
为此使用聚合函数-
mysql> select MAX(Number) from DemoTable;
这将产生以下输出-
+-------------+ | MAX(Number) | +-------------+ | 880 | +-------------+ 1 row in set (0.00 sec)