要查找最新日期,请使用ORDER BY DESC订购日期记录。由于我们只需要3个日期,因此请使用LIMIT 3。
让我们首先创建一个表-
mysql> create table DemoTable ( AdmissionDate date );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('2019-09-04'); mysql> insert into DemoTable values('2019-08-10'); mysql> insert into DemoTable values('2019-09-21'); mysql> insert into DemoTable values('2019-09-18'); mysql> insert into DemoTable values('2019-09-21'); mysql> insert into DemoTable values('2019-08-18'); mysql> insert into DemoTable values('2019-09-18'); mysql> insert into DemoTable values('2019-09-16');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+---------------+ | AdmissionDate | +---------------+ | 2019-09-04 | | 2019-08-10 | | 2019-09-21 | | 2019-09-18 | | 2019-09-21 | | 2019-08-18 | | 2019-09-18 | | 2019-09-16 | +---------------+ 8 rows in set (0.00 sec)
以下是查询以查找表中的最新3个日期-
mysql> select AdmissionDate from (select distinct AdmissionDate from DemoTable) tbl order by AdmissionDate desc limit 3;
这将产生以下输出-
+---------------+ | AdmissionDate | +---------------+ | 2019-09-21 | | 2019-09-18 | | 2019-09-16 | +---------------+ 3 rows in set (0.00 sec)