mysql - 我想要像 sum() 这样的函数在 mysql 中添加数据类型时间列中的所有行

标签 mysql

在我的数据库中有如下表格

+----+-----------+----------+
| id | fk_id     | duration |
+----+-----------+----------+
|  1 |        23 | 00:00:31 |
|  2 |        23 | 00:00:36 |
|  3 |       677 | 00:00:36 |
|  4 |       678 | 00:00:36 |
+----+-----------+----------+

在上表中,持续时间列的数据类型为时间。

下面是那个表的schema

Create Table: CREATE TABLE `durationof` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `fk_id` int(11) NOT NULL,
  `duration` time NOT NULL,
  PRIMARY KEY (`id`))

我只想在查询的持续时间列中添加所有时间,我该怎么做?

就像求和函数一样,有没有把所有mysql时间值相加的函数。

我尝试从 durationof 中选择 addtime(diration);

但这不起作用。

最佳答案

我完全不同意@Corbin's original answer .如 The TIME Type 下所述:

TIME values may range from '-838:59:59' to '838:59:59'. The hours part may be so large because the TIME type can be used not only to represent a time of day (which must be less than 24 hours), but also elapsed time or a time interval between two events (which may be much greater than 24 hours, or even negative).

对所有此类区间求和:convert to seconds , take the sum然后 convert back again .

SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(duration))) FROM durationof

查看sqlfiddle .

关于mysql - 我想要像 sum() 这样的函数在 mysql 中添加数据类型时间列中的所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13430635/

相关文章:

mysql - 如何启用我的 innodb

mysql - 将 Java Web 应用程序与 jboss、mysql 和 activemq 一起打包以进行部署

mysql - 选择字段的不同子字符串值并计算该选择中字符的实例数

mysql - 将值插入空列

mysql - 如何通过 Laravel 迁移触发每一行的 MySQL 过程

python - mysql-python 的 bigint 格式说明符

mysql - Spring + MyBatis - 设置数据源

mysql - MySQL 数据库事务是否会破坏使用 RefreshDatabase 或 DatabaseTransactions 的 Laravel PHPUnit 测试?

mysql 可以将逗号值拆分为单个新列吗?

PHP MySQL 登录问题