从SQL中选择后的列的总和

时间:2017-09-06 07:04:58

标签: php mysql sql

我正在尝试从表中选择一列,不仅显示每行的值,还根据查询显示列的总和。
如下面提供的代码所示,每行都显示完美,但是,当到达列的总数时,只有第一个被调用行的值显示为总数。
请看下面提供的图片。仅显示值为25:18的第一行作为总计 表格底部的预期总值应为51:19 + 50:41 + 25:18,但总数再次只是第一行(DESC)。请指教

table display after selected from sql

if (($user != "") && ($date !="")){
    $mysql=("SELECT inet_ntoa(conv(hex(location_ip), 16, 10)) as ip, conv(hex(idvisitor), 16, 10) as visitorId, 
    visit_last_action_time, visit_first_action_time, visitor_localtime,  user_id, visit_total_time
    FROM piwik_log_visit WHERE user_id='".$user."' AND date(visit_first_action_time) ='".$date."'
    Order by visit_first_action_time DESC");}

$query= mysqli_query($conn, $mysql) or die ("SQL Error: ".mysqli_error($conn));
while ($row = mysqli_fetch_array($query))
        {
            $time =date('F d, Y | h:i:s a', strtotime($row['visitor_localtime']));
            $time =date('h:i:s a', strtotime($row['visitor_localtime']));
            $idaction_url = $row['idaction_url'];
            $ip =$row['ip'];
            $visitor = $row['visitorId'];

            $first = date('F d, Y |', strtotime($row['visit_first_action_time']));
            $date = $first.' '.$time;

            ///total time added
            $sum = array($row['visit_total_time']);
            $sum_total = array_sum($sum);
            $sum_time = gmdate('H:i:s', $sum_total);    

            $total = gmdate('i:s', $row['total_time']); 

2 个答案:

答案 0 :(得分:0)

你可以尝试

$sum_total = 0;
foreach($sum as $key=>$value)
{
   $sum_total+= $value;
}
echo $sum_total;

答案 1 :(得分:0)

我有足够的时间来完成这段代码:

$sql=("SELECT SUM(total_visit_time) as total_time....);

while ($row=mysqli_fetch_assoc($sql)){

$total_time_spent += $row['total_time'];
}

echo $total_time_spent;