将 MySQL 数据库表中的日期格式改为 d/m/y
以下是语法 -
select date_format(yourColumnName,'%d/%m/%Y') as anyAliasName from yourTableName;
让我们首先创建一个表 -
mysql> create table DemoTable ( AdmissionDate date ); Query OK, 0 rows affected (0.89 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable values('2019-01-21'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('2019-09-09'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('2018-12-31'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('2017-11-01'); Query OK, 1 row affected (0.16 sec)
使用 select 语句显示表中的所有记录 -
mysql> select *from DemoTable;
这将产生以下输出 -
+---------------+ | AdmissionDate | +---------------+ | 2019-01-21 | | 2019-09-09 | | 2018-12-31 | | 2017-11-01 | +---------------+ 4 rows in set (0.00 sec)
以下是将日期格式更改为 d/m/y 格式的查询 -
mysql> select date_format(AdmissionDate,'%d/%m/%Y') as AdmissionDate from DemoTable;
这将产生以下输出 -
+---------------+ | AdmissionDate | +---------------+ | 21/01/2019 | | 09/09/2019 | | 31/12/2018 | | 01/11/2017 | +---------------+ 4 rows in set (0.00 sec)
广告