简体   繁体   English

计算每天/每天每个用户的总时间-php / mysql

[英]Calculate total time of each user in single/per day - php/mysql

     > Start                    stop                  pinnumber
     > ---------------------------------------------------------
     > 2012-03-14 13:22:17    2012-03-14 15:22:50      2001
     > 2012-03-14 18:11:10    2012-03-14 19:10:10      2001
     > 2012-03-15 07:20:10    2012-03-15 13:20:50      2001
     >**2012-03-16 19:21:55       2012-03-17 02:55:22  2001** //on 16(19:21:55
                                                                     to 23:59:59) and     
                                                                 //on 17(00 to 02:55:22) 
     > 2012-03-17 14:15:05    2012-03-17 17:44:50      2001
     > 2012-03-18 19:11:10    2012-03-18 19:10:10      2002
     > 2012-03-18 10:20:10    2012-03-18 13:20:50      2003
     > 2012-03-18 11:20:10    2012-03-18 15:11:50      2001

Question: 题:

How can I calculate total time of each user of per day ('start', 'stop') per day? 如何计算每天每个用户每天的总时间(“开始”,“停止”)? Please see the above highlighted point. 请查看以上突出显示的点。 Suppose, If user 'start' today and stop it tomorrow then today hour are different and tomorrow hour are different? 假设,如果用户今天“开始”并且明天“停止”,那么今天的时间是否不同,明天的时间是否不同?

right now i am using following query:- 现在我正在使用以下查询:

SELECT SEC_TO_TIME( SUM( TIME_TO_SEC(TIMEDIFF( stop , start ) ) ) ) AS time1, clock. 选择SEC_TO_TIME(SUM(TIME_TO_SEC(TIMEDIFF( stopstart ))))))以time1作为时钟。 * FROM table_name WHERE pin_number = '2001' GROUP BY DATE_FORMAT( start , '%W %M %Y' ) * FROM table_name WHERE pin_number =' pin_number BY DATE_FORMAT( start ,'%W%M%Y')

from above query i am getting per day records but when start date and stop date is different. 从上面的查询中,我每天都在获取记录,但是开始日期和结束日期不同。 it calculate total time not single day time but i need per day time. 它计算的总时间不是单日时间,而是我每天所需的时间。

I think I finally got there. 我想我终于到了。 You first need to get a set of days, which I obtain through a subquery that takes a UNION of the start and stop times (you could filter this for your pinnumber if desired in order to reduce the size of the JOIN ). 首先,您需要获取一组日期,我可以通过一个子查询来获得该日期,该子查询采用了UNION的开始时间和停止时间(如果需要,可以将其过滤为您的pinnumber ,以减小JOIN的大小)。

One then joins each such date with those (start,stop) pairs that encompass that date (ie either start during the day, or the start of the day is between the start and stop time). 然后,将每个这样的日期与包含该日期的那些(start,stop)对连接(即,在一天中开始,或者一天中的开始在开始和停止时间之间)。

Finally, one groups by day and takes the sum of the amount of time between the start and end times, cutting off at the day start and end as appropriate (the magic 86400 is the number of seconds in a day = 24*60*60). 最后,按天分组,计算开始时间和结束时间之间的时间总和,并视情况在一天的开始和结束时间进行截断(魔术86400是一天中的秒数= 24 * 60 * 60 )。 Sadly this won't play nice with daylight savings, leap seconds, etc... 遗憾的是,这与夏时制,leap秒等效果不佳。

SELECT FROM_UNIXTIME(unixday, '%d/%m/%Y'), SUM(
    LEAST(   unixday+86400, UNIX_TIMESTAMP(Stop ))
  - GREATEST(unixday      , UNIX_TIMESTAMP(Start))
) AS Seconds
FROM table_name JOIN (

  SELECT UNIX_TIMESTAMP(DATE(Start)) AS unixday FROM table_name
UNION 
  SELECT UNIX_TIMESTAMP(DATE(Stop )) AS unixday FROM table_name

) AS days ON (
      UNIX_TIMESTAMP(Start) BETWEEN unixday AND unixday+86400
  OR (unixday BETWEEN UNIX_TIMESTAMP(Start) AND UNIX_TIMESTAMP(Stop))
)
WHERE pinnumber = 2001
GROUP BY unixday;

See it on sqlfiddle . sqlfiddle上看到它。

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

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