要仅从DateTime获取日期,请使用日期格式说明符-
%d for day %m for month %Y for year
让我们首先创建一个表-
mysql> create table DemoTable ( AdmissionDate datetime );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('2019-07-21 12:34:56'); mysql> insert into DemoTable values('2016-08-18 10:00:02'); mysql> insert into DemoTable values('2018-01-03 11:02:20');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+---------------------+ | AdmissionDate | +---------------------+ | 2019-07-21 12:34:56 | | 2016-08-18 10:00:02 | | 2018-01-03 11:02:20 | +---------------------+ 3 rows in set (0.00 sec)
这是仅获取日期的查询-
mysql> select date_format(AdmissionDate,'%d.%m.%Y') AS Date from DemoTable;
这将产生以下输出-
+------------+ | Date | +------------+ | 21.07.2019 | | 18.08.2016 | | 03.01.2018 | +------------+ 3 rows in set (0.00 sec)