要设置特定的日期格式,您需要在MySQL中使用DATE_FORMAT()。让我们首先创建一个表-
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, ArrivalDate date );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(ArrivalDate) values('2019-01-31'); mysql> insert into DemoTable(ArrivalDate) values('2019-04-26'); mysql> insert into DemoTable(ArrivalDate) values('2019-03-01');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+----+-------------+ | Id | ArrivalDate | +----+-------------+ | 1 | 2019-01-31 | | 2 | 2019-04-26 | | 3 | 2019-03-01 | +----+-------------+ 3 rows in set (0.00 sec)
以下是在MySQL中设置特定日期格式的查询-
mysql> select Id,DATE_FORMAT(ArrivalDate, '%d-%M-%Y') from DemoTable;
这将产生以下输出-
+----+--------------------------------------+ | Id | DATE_FORMAT(ArrivalDate, '%d-%M-%Y') | +----+--------------------------------------+ | 1 | 31-January-2019 | | 2 | 26-April-2019 | | 3 | 01-March-2019 | +----+--------------------------------------+ 3 rows in set (0.06 sec)