在 MySQL 中将分钟数 INT 转换为 TIME?

mysqlmysqli database

要在 MySQL 中将分钟数 INT 转换为 TIME,您可以使用 SEC_TO_TIME() 函数。

语法如下

select SEC_TO_TIME(yourIntColumnName*60) AS `anyAliasName` from yourTableName;

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

mysql> create table convertNumberToMinute
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> NumberToMinute int
   -> );
Query OK, 0 rows affected (0.79 sec)

使用 insert 命令在表中插入一些记录。

查询如下

mysql> insert into convertNumberToMinute(NumberToMinute) values(60);
Query OK, 1 row affected (0.12 sec)
mysql> insert into convertNumberToMinute(NumberToMinute) values(70);
Query OK, 1 row affected (0.21 sec)
mysql> insert into convertNumberToMinute(NumberToMinute) values(80);
Query OK, 1 row affected (0.15 sec)
mysql> insert into convertNumberToMinute(NumberToMinute) values(90);
Query OK, 1 row affected (0.13 sec)
mysql> insert into convertNumberToMinute(NumberToMinute) values(100);
Query OK, 1 row affected (0.22 sec)
mysql> insert into convertNumberToMinute(NumberToMinute) values(110);
Query OK, 1 row affected (0.07 sec)
mysql> insert into convertNumberToMinute(NumberToMinute) values(120);
Query OK, 1 row affected (0.15 sec)

使用 select 语句显示表中的所有记录。

查询如下

mysql> select *from convertNumberToMinute;

以下是输出 −

+----+----------------+
| Id | NumberToMinute |
+----+----------------+
| 1  | 60             |
| 2  | 70             |
| 3  | 80             |
| 4  | 90             |
| 5  | 100            |
| 6  | 110            |
| 7  | 120            |
+----+----------------+
7 rows in set (0.00 sec)

这是在 MySQL 中将分钟数 INT 转换为 TIME 的查询

mysql> select SEC_TO_TIME(NumberToMinute*60) AS `MinuteDemo` from convertNumberToMinute;

以下是输出 −

+------------+
| MinuteDemo |
+------------+
| 01:00:00   |
| 01:10:00   |
| 01:20:00   |
| 01:30:00   |
| 01:40:00   |
| 01:50:00   |
| 02:00:00   |
+------------+
7 rows in set (0.00 sec)

这是显示‘NumberToMinute’列的查询

mysql> select NumberToMinute, SEC_TO_TIME(NumberToMinute*60) AS `MinuteDemo` from convertNumberToMinute;

以下是输出 −

+----------------+------------+
| NumberToMinute | MinuteDemo |
+----------------+------------+
| 60             | 01:00:00   |
| 70             | 01:10:00   |
| 80             | 01:20:00   |
| 90             | 01:30:00   |
| 100            | 01:40:00   |
| 110            | 01:50:00   |
| 120            | 02:00:00   |
+----------------+------------+
7 rows in set (0.00 sec)

相关文章