如何循环遍历 PHP 中的 sql sum 结果?

我需要总结总计列的结果:


SELECT SUM(number_guest) AS total, booking_time

   FROM mt_bookingtable

   WHERE

            merchant_id IN (SELECT merchant_id 

                              FROM mt_merchant 

                              WHERE merchant_id = 1 )

            AND booking_time BETWEEN "12:00" AND "20:00"

            AND

            date_booking like "2020-06-01"

            AND status in ('pending','approved')

   GROUP BY booking_time

结果

我试图像这样使用 foreach:


foreach ($res as $item) {

    $sum += $item['total'];

    return $sum;

}

但我得到了结果null。


守候你守候我
浏览 117回答 1
1回答

哆啦的时光机

删除GROUP BY:SELECT SUM(number_guest) AS totalFROM mt_bookingtableWHERE merchant_id IN (SELECT merchant_id FROM mt_merchant WHERE merchant_id = 1 ) AND      booking_time BETWEEN '12:00' AND '20:00' AND      date_booking like '2020-06-01' AND      status in ('pending', 'approved');
打开App,查看更多内容
随时随地看视频慕课网APP