在MySQL中订购日期格式为“ 01 August 2019”的日期吗?
要显示“2019年8月1日”之类的日期,请使用ORDERBYSTR_TO_DATE()。让我们首先创建一个-
mysql> create table DemoTable1435 -> ( -> DueDate varchar(60) -> );
使用insert在表中插入一些记录-
mysql> insert into DemoTable1435 values('01 August 2019'); mysql> insert into DemoTable1435 values('01 Feb 2018'); mysql> insert into DemoTable1435 values('31 Jan 2017'); mysql> insert into DemoTable1435 values('01 March 2019');
使用选择显示表中的所有记录-
mysql> select * from DemoTable1435;
这将产生以下输出-
+----------------+ | DueDate | +----------------+ | 01 August 2019 | | 01 Feb 2018 | | 31 Jan 2017 | | 01 March 2019 | +----------------+ 4 rows in set (0.00 sec)
以下是在MySQL中以“01August2019”格式订购日期的查询-
mysql> select * from DemoTable1435 -> order by str_to_date(DueDate,'%d %M %Y');
这将产生以下输出-
+----------------+ | DueDate | +----------------+ | 31 Jan 2017 | | 01 Feb 2018 | | 01 March 2019 | | 01 August 2019 | +----------------+ 4 rows in set (0.04 sec)