0

我有这个 PHP 代码:

    $sql="SELECT ticket_seq, SUM(TIMEDIFF(timeend, timestart)) as total FROM ticket_updates GROUP BY ticket_seq";
$rs=mysql_query($sql,$conn) or die(mysql_error());
if(mysql_num_rows($rs) > 0)
{
    while($result=mysql_fetch_array($rs))
    {
        $sql2="SELECT * from tickets where ticketnumber = '".$result["ticket_seq"]."' ";
        $rs2=mysql_query($sql2,$conn) or die(mysql_error());
        $result2=mysql_fetch_array($rs2);

        $sql3="SELECT * from customer where sequence = '".$result2["company"]."' ";
        $rs3=mysql_query($sql3,$conn) or die(mysql_error());
        $result3=mysql_fetch_array($rs3);

        if($result["total"] > $result3["support_hours"])
        {
            echo $result3["company"].' - '.$result["total"].'<br>';
        }
    }
}

但是当我回显 $result["total"] 时,我只会得到随机数。

我正在尝试为多行选择 timestart 和 timeend 之间的时间差并将其相加。

4

2 回答 2

0

我认为这样的事情会解决你的问题

SELECT SUM(
   (SELECT TIMEDIFF(timeend, timestart) FROM ticket_updates GROUP BY ticket_seq)
)
于 2013-03-08T09:31:12.423 回答
0

快速播放和类似的东西可能会做到:-

$sql="SELECT ticket_updates.ticket_seq, customer.company, SUM(CAST((UNIX_TIMESTAMP(timeend) - UNIX_TIMESTAMP(timestart)) AS UNSIGNED)) as total 
    FROM ticket_updates 
    INNER JOIN tickets ON tickets.ticketnumber = ticket_updates.ticket_seq
    INNER JOIN customer ON customer.sequence = tickets.company
    GROUP BY ticket_updates.ticket_seq, customer.company
    HAVING total > (customer.support_hours*60*60)";
$rs=mysql_query($sql,$conn) or die(mysql_error());
if(mysql_num_rows($rs) > 0)
{
    while($result=mysql_fetch_array($rs))
    {
            echo $result3["company"].' - '.$result["total"].'<br>';
    }
}

这是将日期/时间字段转换为 unix 时间戳,找到差异并将其转换为无符号,然后检查支持时间乘以 60 和 60(即,从小时转换为秒)

于 2013-03-08T10:14:20.543 回答