查找每个会话的会话数和总时间

时间:2012-10-17 05:57:53

标签: php mysql

$query=mysql_query("SELECT @rownum := @rownum + 1 AS id,t.convo_id,t.body_xml,FROM_UNIXTIME(t.timestamp) FROM messages t,(SELECT @rownum := 0) r WHERE t.convo_id='137'");

$recordset = array(); 

$i = 0;

while ($row = mysql_fetch_array($query))

{

       $recordset[$i] = $row;
    if ($i > 0) 
    { 
        $recordset[$i-1]['nextid'] = $row['0']; 
        $datetime1 = new DateTime($recordset[$i-1][3]);
        $datetime2 = new DateTime($recordset[$i][3]);

        $interval = $datetime1->diff($datetime2);
        $hours   = $interval->format('%h');
        $minutes = $interval->format('%i');
        $seconds = $interval->format('%s');
        ?>
        <tr>
            <td><?php echo $row[0];?></td>
            <td><?php echo $row[1];?></td>
            <td><?php echo $row[2];?></td>
            <td><?php echo $row[3];?></td>
            <td><?php echo  $hours .":".$minutes.":".$seconds ?></td>
           <?php

     }

    $i++;

}
OUTPUT
-----------------------------------------------------------------------------------
SL.No  Convo               Message                  Date               Duration
         ID
-----------------------------------------------------------------------------------
  2     137             Hi                      2012-08-30 10:29:59     0:0:21
  3     137             How are u?              2012-08-30 10:30:39     0:0:40
  4     137             Fine                    2012-08-30 10:31:05     0:0:26
  5     137             abc                     2012-08-30 15:05:50     2:16:49
  6     137             xyz                     2012-08-30 15:07:03     0:1:13
  7     137             bcc                     2012-08-30 15:07:39     0:0:36

如果持续时间超过10分钟,则将其视为新会话,并记录上次会话的开始时间和结束时间。

我需要找不到。谈话和每次谈话的总时间,如。

----------------------------------------------------------------------------------------
START                             END                          TOTAL TIME
----------------------------------------------------------------------------------------
2012-08-30 10:29:59        2012-08-30 10:31:05                   0.1.37

2 个答案:

答案 0 :(得分:1)

    SELECT @rownum := @rownum + 1 AS id,t.convo_id,t.body_xml,
         str_to_date(min(t.timestamp),'%y%m%d')as
         start,str_to_date(max(t.timestamp),'%y%m%d') as 
end,sum(str_to_date(t.timestamp),'%i')as    duration FROM
         messages t,(SELECT @rownum := 0) r WHERE t.convo_id='137' group by t.convo_id 

答案 1 :(得分:0)

使用以下查询

    SELECT conversation_id, MIN( `startDate` ) as START, MAX( `endDate` ) as END, SUM(`duration`) as TOTALTIME
    FROM `conversation_details`
    GROUP BY conversation_id