从 MySQL 中的时间字段中删除秒数?
mysqlmysqli database
您需要使用 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)