How to combine two rows and calculate the time difference between two timestamp values in MySQL?

前端 未结 6 1014
误落风尘
误落风尘 2020-12-06 06:14

I have a situation that I\'m sure is quite common and it\'s really bothering me that I can\'t figure out how to do it or what to search for to find a relevant example/soluti

6条回答
  •  既然无缘
    2020-12-06 06:42

    Can you change the data collector? If yes, add a group_id field (with an index) into the log table and write the id of the start event into it (same id for start and end in the group_id). Then you can do

    SELECT S.id, S.name, TIMEDIFF(E.ts, S.ts) `diff`
    FROM `log` S
        JOIN `log` E ON S.id = E.group_id AND E.eventtype = 'end'
    WHERE S.eventtype = 'start'
    

提交回复
热议问题