在一个时间间隔内列出来自MySQL服务器的SUM数据

时间:2014-02-20 10:48:52

标签: mysql sql

我有一个名为ch6的表,它有以下字段:

id, daytime, tout, tloss.. etc. 
daytime is DATETIME , tout and tloss are integer

现在,我想检索并列出表格中每个日期tlosstout的总和

date || sum(tout) for certain interval || sum(tloss) for certain interval

我面临一些问题,间隔时间到第二天

例如:

20-2-2014 || (sum of tout from 8:00pm to 8:00am the next day) || (sum of tloss between 8:00pm to 8:00am the next day)

解决方案是什么?请帮忙。

1 个答案:

答案 0 :(得分:0)

您可以尝试以下

SELECT daytime , SUM(tout) AS total_tout , SUM(tloss) as total_loss 
FROM ch6 
WHERE daytime 
BETWEEN CURRENT_DATE() + INTERVAL 20 HOUR AND CURRENT_DATE() + INTERVAL 32 HOUR
GROUP BY daytime;