Calculating time difference in MySQL but taking events into account -
i have table structured this
index - date ------ time ----- status 1 2015-01-01 13:00:00 start 2 2015-01-01 13:10:00 continue 3 2015-01-01 13:20:00 continue 4 2015-01-01 13:30:00 end 5 2015-01-01 13:30:00 ready 6 2015-01-01 13:40:00 start 7 2015-01-01 13:50:00 continue 8 2015-01-01 15:00:00 end
and count time between start , end (ie. index 1-4 30min, 6-8 20min), taking account first start , first end, query doesn't choose time difference of index 1-8. query used calculate time difference between 2 statues (start-end) , show result multiple instances of start-end without them getting batched 1 event?
for each start, use query next end time. then, calculate difference. logic this:
select t.*, timestampdiff(second, dt, next_dt) (select t.*, addtime(t.date, t.time) dt, (select addtime(t2.date, t2.time) table t2 addtime(t2.date, t2.time) > addtime(t.date, t.time) , status = 'end' order addtime(t2.date, t2.time) desc limit 1 ) next_dt table t status = 'start' ) t
this assumes date , time columns stored using proper database types (date
, time
). if storing them else, have needlessly complicate logic convert them internal formats.
Comments
Post a Comment