在几分钟内将整数INT转换为TIME?
要将INT中的数字(以分钟为单位)转换为TIME(在MySQL中),可以使用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 -> );
使用insert命令在表中插入一些记录。
查询如下
mysql> insert into convertNumberToMinute(NumberToMinute) values(60); mysql> insert into convertNumberToMinute(NumberToMinute) values(70); mysql> insert into convertNumberToMinute(NumberToMinute) values(80); mysql> insert into convertNumberToMinute(NumberToMinute) values(90); mysql> insert into convertNumberToMinute(NumberToMinute) values(100); mysql> insert into convertNumberToMinute(NumberToMinute) values(110); mysql> insert into convertNumberToMinute(NumberToMinute) values(120);
使用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)