如何查找列中的最大数字?

mysqlmysqli database

您可以借助聚合函数 MAX 查找列中的最大数字。语法如下 −

select max(yourColumnName) as anyVariableName from yourTableName;

为了理解上述概念,让我们创建一个带有 int 列的表。以下是创建表的查询。

mysql> create table HighestNumberDemo
   −> (
      −> BigNumber int
   −> );
Query OK, 0 rows affected (0.87 sec)

现在在表中插入一些值。插入记录的查询如下 −

mysql> insert into HighestNumberDemo values(1234);
Query OK, 1 row affected (0.43 sec)

mysql> insert into HighestNumberDemo values(9999);
Query OK, 1 row affected (0.18 sec)

mysql> insert into HighestNumberDemo values(10000);
Query OK, 1 row affected (0.17 sec)

mysql> insert into HighestNumberDemo values(989898);
Query OK, 1 row affected (0.14 sec)

mysql> insert into HighestNumberDemo values(999987);
Query OK, 1 row affected (0.18 sec)

现在您可以借助 select 语句显示所有记录。显示所有记录的查询如下 −

mysql> select *from HighestNumberDemo;

以下是输出 −

+-----------+
| BigNumber |
+-----------+
|      1234 |
|      9999 |
|     10000 |
|    989898 |
|    999987 |
+-----------+
5 rows in set (0.00 sec)

实现我们上面讨论的语法来查找列中的最高数字。查询如下 −

mysql> select max(BigNumber) as HighestNumber from HighestNumberDemo;

以下是输出 −

+---------------+
| HighestNumber |
+---------------+
| 999987        |
+---------------+
1 row in set (0.00 sec)

相关文章