如何在 MySQL 中获取行数据的最大值 (id)?
mysqlmysqli database更新于 2025/10/3 17:16:17
要获取最大值 (id),请在 MySQL 中使用 MAX() 方法。以下是语法 −
select MAX(yourColumnName) AS anyAliasName from yourTableName;
首先我们创建一个表 −
mysql> create table DemoTable710 (Id int); Query OK, 0 rows affected (0.53 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable710 values(1001); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable710 values(2001); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable710 values(1998); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable710 values(1789); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable710 values(1678); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable710 values(9087); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable710 values(1908); Query OK, 1 row affected (0.17 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable710;
这将产生以下输出 -
+------+ | Id | +------+ | 1001 | | 2001 | | 1998 | | 1789 | | 1678 | | 9087 | | 1908 | +------+ 7 rows in set (0.00 sec)
以下是在 MySQL 中获取行数据最大值(id)的查询 −
mysql> select MAX(Id) AS Max_Id from DemoTable710;
这将产生以下输出 -
+--------+ | Max_Id | +--------+ | 9087 | +--------+ 1 row in set (0.00 sec)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

