您可以使用ORDER BY STR_TO_DATE()。让我们首先创建一个表-
mysql> create table DemoTable -> ( -> DueTime varchar(20) -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('7:30AM'); mysql> insert into DemoTable values('9:00PM'); mysql> insert into DemoTable values('11:00PM');
使用select语句显示表中的所有记录-
mysql> select * from DemoTable;
+---------+ | DueTime | +---------+ | 7:30AM | | 9:00PM | | 11:00PM | +---------+ 3 rows in set (0.00 sec)
这是查询以获取最近时间的记录-
mysql> select * from DemoTable -> order by str_to_date(DueTime, '%h:%i%p') desc limit 1;
这将产生以下输出-
+---------+ | DueTime | +---------+ | 11:00PM | +---------+ 1 row in set (0.00 sec)