从 MySQL 中的 varchar 字段获取最大值
mysqlmysqli database更新于 2025/9/15 18:20:17
为此使用 MAX() 函数和 SUBSTRING()。让我们首先创建一个表 −
mysql> create table DemoTable -> ( -> Id varchar(200) -> ); Query OK, 0 rows affected (0.52 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable values('2019-0515-1980');
Query OK, 1 row affected (0.49 sec)
mysql> insert into DemoTable values('2019-0516-780');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable values('2019-0517-2780');
Query OK, 1 row affected (0.16 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable;
输出
+----------------+ | Id | +----------------+ | 2019-0515-1980 | | 2019-0516-780 | | 2019-0517-2780 | +----------------+ 3 rows in set (0.00 sec)
以下是从 varchar 字段获取最大值的查询 −
mysql> select max(Id) from DemoTable -> where Id LIKE CONCAT(SUBSTRING(Id, 1, 4),'%');
输出
+----------------+ | max(Id) | +----------------+ | 2019-0517-2780 | +----------------+ 1 row in set (0.00 sec)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

