PHP选择timediff与select查询和逐个用户

时间:2013-09-10 08:05:22

标签: php mysql sql

我有这个SQL查询:

$sql="SELECT *, COUNT(assigned_to) AS my_groupcount from tickets where deleted = '' and DAY(datetime) = '".$_GET["d"]."' and MONTH(datetime) = '".$_GET["m"]."' and YEAR(datetime) = '".$_GET["Y"]."' group by assigned_to order by datetime ASC ";
    $rs=mysql_query($sql,$conn);
    while($result=mysql_fetch_array($rs))
    {
        //work out the total time taken
        $sql3="SELECT *,  TIMEDIFF( timeend, timestart ) AS support_time_used FROM ticket_updates WHERE ticket_seq = '".$result["ticketnumber"]."' ";
        $rs3=mysql_query($sql3,$conn) or die (mysql_error());
        $totaltime = 0;
        while($result3=mysql_fetch_array($rs3))
        {
            $totaltime = $totaltime+substr($result3["support_time_used"],0,2)*60+substr($result3["support_time_used"],3,2);
        }   
        $hours=intval($totaltime / 60);
        $minutes=$totaltime -($hours * 60);
        $total_ticket_time = $hours.'h '.$minutes.'m';

        echo $result["assigned_to"].' ('.$result["my_groupcount"].') - Time: '.$total_ticket_time.'<br>';
    }

所以它从ticket表中选择所有行并按asigned_to列进行分组。

然后计算出ticket_updates表中每个用户使用的时间。

ticket_updates.ticket_seq列链接到tickets.ticketnumber列,ticket_updates表中可能只有一行或多行,表单中有1行,因此它会在{{{ 1}}表。

我试图从故障单表中列出assigned_to(每个),并在每个故障表旁边放置他们已经使用了多少时间,但它只选择它形成一张票。

如何从所有门票中选择?

1 个答案:

答案 0 :(得分:1)

你不需要分两步完成,是吗。

select assigned_to, sum(TIMEDIFF( timeend, timestart )) as total_time, count(distinct ticket_id) as ticket_count from ticket_update where ticket_id in (select ticket_id from ticket where condition)
group by assigned_to

编辑:你确实需要:

        select ticket.assigned_to, 
sum(TIMEDIFF( ticket_update_timeend, ticket_update.timestart )) as total_time, count(distinct ticket.ticket_id) as ticket_count 
from 
    ticket_update 
    inner join ticket
    on ticket.ticket_id=ticket_update.ticket_id
where ticket.deleted = '' and DAY(ticket.datetime) = '".$_GET["d"]."' and MONTH(ticket.datetime) = '".$_GET["m"]."' and YEAR(ticket.datetime) = '".$_GET["Y"]."'
    group by ticket.assigned_to