简体   繁体   English

按两个不同的日期和时间分组mysql

[英]Group by two different date and time mysql

I need to generate a report based on date and time . 我需要根据date and time生成报告。

My database table has payments recorded with amount and created datetime . 我的数据库表包含记录amountcreated datetime付款。

And, I need to group data like sum of payments from 9:30pm yesterday to 9:29pm today 而且,我需要将昨天9:30pm到今天9:30pm 9:29pm汇总数据分组

(ie) instead of group by date(created_date). (即)而不是按日期分组(created_date)。 And then records between these time range . 然后记录这些time range

I have the last two months records in my payments table. 我的付款表中有最近两个月的记录。

Any help will be appreciated. 任何帮助将不胜感激。

Thanks 谢谢

Just add 2.5 hours to each date/time. 每个日期/时间只需2.5小时。 This is most easily represented at 150 minutes: 这最容易在150分钟出现:

group by date(created_date + interval 150 minute) 

Thanks for all the answers. 感谢所有的答案。

I ended up with this one - group by 24 hours 我结束了这一组 - 24小时

SELECT CONCAT( CONCAT(DATE(DATE_SUB(created, INTERVAL '21:30' HOUR_MINUTE)),
' 21:30:00') , ' - ' ,CONCAT(DATE(DATE_ADD(created, INTERVAL '2:30' HOUR_MINUTE)),
'  21:29:59') ) AS tintin, COUNT(*)  FROM test GROUP BY DATE(DATE_SUB(created, 
INTERVAL '21:30' HOUR_MINUTE)) ORDER BY DATE(created) DESC ; 

got some idea from here 从这里得到一些想法

Mysql Group By 24 hour intervals Mysql组每隔24小时

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM