从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) -> );
使用insert命令在表中插入一些记录。查询如下:
mysql> insert into removeSecondsFromTime(StartTime,EndTime) values('10:20:45','11:21:40'); mysql> insert into removeSecondsFromTime(StartTime,EndTime) values('12:30:55','13:20:21'); mysql> insert into removeSecondsFromTime(StartTime,EndTime) values('14:40:57','15:55:02');
使用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)