我有一个表"services"
,其中包含两列up(boolean type)
和readingTime(DateTime type)
。现在我想计算向上列值为TRUE的总小时数。在Rails / Mysql
答案 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;