如何获得具有结果集的列的总和?

时间:2018-04-13 03:23:40

标签: php mysqli

如何从返回的结果中返回一列中所有值的总和?例如,我想在每一行中加总重量并将其作为总重量返回?

const jwtOptions = {  
    jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
    secretOrKey: process.env.JWT_SECRET
};

1 个答案:

答案 0 :(得分:1)

只需在循环中添加权重。

$requested_Date = date("$pick_up_year/$pick_up_month/$pick_up_day");                
$sql= "SELECT users.user_id, users.user_first, users.user_phone, requests.req_id, weight FROM users INNER join requests on 
       users.user_id=requests.user_id where pick_up_date = '$requested_Date'";    
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
echo "<table>
    <tr>
        <th>Customer ID</th>
        <th>Customer Name</th>
        <th>Customer Phone Number</th>
        <th>Request ID</th>
        <th>Weight</th>
    </tr>";
if($resultCheck > 0){
    $totalWeight = 0;
    while($row = mysqli_fetch_assoc($result)){
        $totalWeight += $row['weight']; 
echo'<tr>
    <td>'.$row['user_id'].'</td>
    <td>'.$row['user_first'].'</td>
    <td>'.$row['user_phone'].'</td>
    <td>'.$row['req_id'].'</td>
    <td>'.$row['weight'].'</td>
</tr>';                          
}    
echo '</table>'.$totalWeight;