如何在 MySQL 中仅显示小时和分钟?
仅显示小时和分钟,请使用 DATE_FORMAT() 并设置格式说明符,语法如下 −
select date_format(yourColumnName,'%H:%i') as anyAliasName from yourTableName;
首先,我们创建一个表 −
mysql> create table DemoTable1527 -> ( -> ArrivalDatetime datetime -> ); Query OK, 0 rows affected (0.76 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable1527 values('2019-01-10 12:34:45'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1527 values('2018-12-12 11:00:34'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1527 values('2019-03-21 04:55:56'); Query OK, 1 row affected (0.18 sec)
使用 select 语句从表中显示所有记录 −
mysql> select * from DemoTable1527;
这会产生以下输出 −
+---------------------+ | ArrivalDatetime | +---------------------+ | 2019-01-10 12:34:45 | | 2018-12-12 11:00:34 | | 2019-03-21 04:55:56 | +---------------------+ 3 rows in set (0.00 sec)
以下是仅显示小时和分钟的查询 −
mysql> select date_format(ArrivalDatetime,'%H:%i') as HourAndMinutes from DemoTable1527;
这会产生以下输出 −
+----------------+ | HourAndMinutes | +----------------+ | 12:34 | | 11:00 | | 04:55 | +----------------+ 3 rows in set (0.00 sec)
广告