如何在php中循环sql sum结果?

5lhxktic  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(371)

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

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 .

m1m5dgzv

m1m5dgzv1#

拆下 GROUP BY :

SELECT SUM(number_guest) AS total
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');

相关问题