您可以使用STR_TO_DATE()函数。让我们首先创建一个表-
mysql> create table DemoTable ( AdmissionDate varchar(200) );
使用插入命令在表中插入记录-
mysql> insert into DemoTable values('12-01-2019'); mysql> insert into DemoTable values('14-12-2016'); mysql> insert into DemoTable values('26-04-2018'); mysql> insert into DemoTable values('31-05-2013');
使用select语句显示表中的所有记录-
mysql> select * from DemoTable;
这将产生以下输出-
+---------------+ | AdmissionDate | +---------------+ | 12-01-2019 | | 14-12-2016 | | 26-04-2018 | | 31-05-2013 | +---------------+ 4 rows in set (0.00 sec)
以下是按日期ASC排序的查询-
mysql> select *from DemoTable ORDER BY STR_TO_DATE(AdmissionDate,'%d-%m-%Y') ASC;
这将产生以下输出-
+---------------+ | AdmissionDate | +---------------+ | 31-05-2013 | | 14-12-2016 | | 26-04-2018 | | 12-01-2019 | +---------------+ 4 rows in set (0.00 sec)