獲取 MySQL 中 varchar 欄位的最大值
為此,請使用 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)
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言
C++
C#
MongoDB
MySQL
Javascript
PHP