如何在分钟内将HH:MM:SS格式的varchar转换为int值。使用 mysql

How do you convert a varchar in the format of HH:MM:SS to a int value in minutes. Using mysql

我正在尝试将 HH:MM:SS 格式的 varchar 转换为表示总分钟数的 int 值。 table 有一个名为:'duration' 的 varchar 和名为:'amount_of_students'.

的学生总数

我想用 class 的持续时间除以学生人数。

我已经找到了下面的代码,但这在 MySQL 中不起作用,而且似乎只在 MS SQL 中起作用:

select duration,
       (convert(int, left(duration, locate(':', duration) - 1)) * 60 +
        convert(int, left(right(duration, 5), 2))
       )
from Class

我目前收到以下错误:

Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'int, left(duration, locate(':', duration) - 1)) * 60 + convert(int, le'

如有任何帮助,我们将不胜感激。

在 MySQL 中,您可以在此类 HH:MM:SS 格式的字符串上使用函数 hourminute:它们将被解释为时间值:

select hour(duration) * 60 + minute(duration)
from Class

您可以使用 time_to_sec() 并乘以 60。对于十进制值:

select time_to_sec(duration) / 60

对于整数:

select floor(time_to_sec(duration) / 60)