如何根据列值计算记录之间的小时数

时间:2016-09-06 08:59:28

标签: mysql ruby-on-rails

我有一个表"services",其中包含两列up(boolean type)readingTime(DateTime type)。现在我想计算向上列值为TRUE的总小时数。在Rails / Mysql

中执行此操作的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

您可能想要使用TIMESTAMPDIFF

SELECT *, TIMESTAMPDIFF(HOUR, readingTime, now()) AS hours
FROM services
WHERE up = true;

修改

如果您希望指定日期范围的总小时数通过日期

SELECT *, TIMESTAMPDIFF(HOUR, readingTime, now()) AS hours
FROM services
WHERE up = true
AND (reading_on BETWEEN startDate AND endDate;