从 MySQL 的 varchar 日期列表中获取最大日期
让我们首先创建一个表 -
mysql> create table DemoTable ( AdmissionDate varchar(100) ); Query OK, 0 rows affected (0.76 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable values('Sunday, 11 August 2019'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('Friday, 18 October 2019'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Thursday, 18 July 2019'); Query OK, 1 row affected (0.23 sec)
使用 select 语句从表中显示所有记录 -
mysql> select *from DemoTable ;
这将生成以下输出 -
+-------------------------+ | AdmissionDate | +-------------------------+ | Sunday, 11 August 2019 | | Friday, 18 October 2019 | | Thursday, 18 July 2019 | +-------------------------+ 3 rows in set (0.00 sec)
以下是查询从上面插入的 varchar 日期列表中获得最大日期 -
mysql> select max(str_to_date(AdmissionDate,'%W, %d %M %Y')) AS MaxDate from DemoTable;
这将生成以下输出 -
+------------+ | MaxDate | +------------+ | 2019-10-18 | +------------+ 1 row in set (0.04 sec)
广告