在MySQL中比较日期字符串
要比较日期字符串,请使用MySQL中的STR_TO_DATE()函数。
首先创建一个表 –
mysql> create table DemoTable712 (
Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
ArrivalDate varchar(100)
);
Query OK, 0 rows affected (0.65 sec)
使用insert命令将一些记录插入到表中 –
mysql> insert into DemoTable712(ArrivalDate) values('10.01.2019');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable712(ArrivalDate) values('11.12.2018');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable712(ArrivalDate) values('01.11.2017');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable712(ArrivalDate) values('20.06.2016');
Query OK, 1 row affected (0.23 sec)
使用select语句显示表中的所有记录 –
mysql> select *from DemoTable712;
这将产生以下输出 –
+----+-------------+
| Id | ArrivalDate |
+----+-------------+
| 1 | 10.01.2019 |
| 2 | 11.12.2018 |
| 3 | 01.11.2017 |
| 4 | 20.06.2016 |
+----+-------------+
4 rows in set (0.00 sec)
下面是比较日期字符串的查询语句 –
mysql> select *from DemoTable712 where str_to_date(ArrivalDate,'%d.%m.%Y')='2017-11-01';
这将产生以下输出 –
+----+-------------+
| Id | ArrivalDate |
+----+-------------+
| 3 | 01.11.2017 |
+----+-------------+
1 row in set (0.00 sec)
阅读更多:MySQL 教程