如何通过 MySQL 查询更改表列中的日期格式?
若要更改日期格式,请使用 DATE_FORMAT() 函数。让我们先创建一个表 -
mysql> create table DemoTable1906 ( DueTime datetime ); Query OK, 0 rows affected (0.00 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable1906 values(now()); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1906 values(curdate()); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1906 values('2017-10-11'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1906 values('2015-01-12'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1906 values('2018-04-25'); Query OK, 1 row affected (0.00 sec)
使用 select 语句显示表中的所有记录 -
mysql> select * from DemoTable1906;
这将生成以下输出 -
+---------------------+ | DueTime | +---------------------+ | 2019-12-11 13:54:22 | | 2019-12-11 00:00:00 | | 2017-10-11 00:00:00 | | 2015-01-12 00:00:00 | | 2018-04-25 00:00:00 | +---------------------+ 5 rows in set (0.00 sec)
以下是更改日期格式的查询 -
mysql> select date_format(DueTime,'%d-%m-%Y') from DemoTable1906;
这将生成以下输出 -
+---------------------------------+ | date_format(DueTime,'%d-%m-%Y') | +---------------------------------+ | 11-12-2019 | | 11-12-2019 | | 11-10-2017 | | 12-01-2015 | | 25-04-2018 | +---------------------------------+ 5 rows in set (0.00 sec)
广告