mysql - Sum the time in field type varchar
问题描述
i have a database with Id(int),name(varchar),Time(varchar)
the time is in this format (HH:MM)
and i want to sum the time for every Id
i tried this but it returns an error in Mysql :
SELECT Id,
SUM( 60 * CONVERT(int, LEFT( Time, 2)) + CONVERT(int, RIGHT(Time, 2)) ) AS
TotalMinutes
FROM retards
GROUP BY Id;
can someone tell me what's wrong?
this is the error, it's a syntax error as it returns :
MySQL a répondu : Documentation
1064 - Erreur de syntaxe près de 'int, LEFT( total, 2)) + CONVERT(int, RIGHT(total, 2)) ) AS TotalMinutes
FROM re' à la ligne 2
解决方案
Wouldn't be better using of direct conversion?
TIME_TO_SEC(CONVERT(`Time`,time)) / 60 AS `TotalMinutes`
full query:
SELECT Id,
SUM(TIME_TO_SEC(CONVERT(`Time`,time)) / 60) AS
`TotalMinutes`
FROM retards
GROUP BY Id;
推荐阅读
- javascript - 直接将对象属性存储在对象数组中,无需在 javascript 中创建完整对象
- gps - 如何使用 KumarRobotics / ublox 包更快地接收 gps 数据?
- php - 在 React 中通过提交 Formik 表单通过 wp_mail() 发送电子邮件
- django - 我无法添加外键值
- java - 使用 Thymeleaf 和 spring 更新“电影”数据的问题
- java - 这是什么类型的字符:以及如何在java中处理它
- python - 将 Python 服务器连接到后端 C++
- netlogo - Netlogo:处理行为不端的算术运算符“<”
- swift - 如何使用枚举和关联的自定义对象读取和写入对象?
- javascript - 错误“无法注册 ServiceWorker:文档处于无效状态”是什么意思。意思是?