移除 MySQL 中时间字段中的秒?


你需要使用 TIME_FORMAT() 来移除时间字段中的秒。语法如下

SELECT TIME_FORMAT(yourColumnName1, "%H:%i") AS anyVariableName,
TIME_FORMAT(yourColumnName2, "%H:%i") AS anyVariableName
FROM yourTableName;

为了理解上述语法,让我们创建一个表。创建表的查询如下

mysql> create table removeSecondsFromTime
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> StartTime time,
   -> EndTime time,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.71 sec)

使用 insert 命令向表中插入一些记录。查询如下

mysql> insert into removeSecondsFromTime(StartTime,EndTime) values('10:20:45','11:21:40');
Query OK, 1 row affected (0.43 sec)
mysql> insert into removeSecondsFromTime(StartTime,EndTime) values('12:30:55','13:20:21');
Query OK, 1 row affected (0.31 sec)
mysql> insert into removeSecondsFromTime(StartTime,EndTime) values('14:40:57','15:55:02');
Query OK, 1 row affected (0.24 sec)

使用 select 语句显示表中的所有记录。查询如下

mysql> select *from removeSecondsFromTime;

以下是输出;

+----+-----------+----------+
| Id | StartTime | EndTime  |
+----+-----------+----------+
|  1 | 10:20:45  | 11:21:40 |
|  2 | 12:30:55  | 13:20:21 |
|  3 | 14:40:57  | 15:55:02 |
+----+-----------+----------+
3 rows in set (0.00 sec)

以下是移除时间字段中秒的查询

mysql> select TIME_FORMAT(StartTime, "%H:%i") as StartingTime,
   -> TIME_FORMAT(EndTime, "%H:%i") as EndingTime
   -> from removeSecondsFromTime;

以下是输出;

+--------------+------------+
| StartingTime | EndingTime |
+--------------+------------+
| 10:20        | 11:21      |
| 12:30        | 13:20      |
| 14:40        | 15:55      |
+--------------+------------+
3 rows in set (0.00 sec)

更新于:30-Jul-2019

2K+ 浏览量

开启您的 职业生涯

完成课程,获得认证

开始
广告