Parkolo11
Parkolo11

Reputation: 113

Sum all the rows in sql after i select a start and end date

I select a start and end date, and after that, i would like to sum all the orders.

If we take a look at my image, the sum value will be 80. From 2017-09-02 to 2017-09-10, there was 80 order.

And i want to echo that sum value with php.

My sql code:

$sql = "
                        SELECT datum, COUNT(rendeles_id) AS ennyi FROM rendeles_adatok 

                            WHERE datum >= '$date_start' AND (datum <= '$date_end' AND status = $rendeles_allapot)

                        GROUP BY datum ORDER BY $kereses_sorrend
                   ";

The "ennyi" alias is that i echo out in the Orders on that day column. (last column on the photo)

enter image description here

Upvotes: 0

Views: 127

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269623

If you just want the overall total, remove the GROUP BY:

SELECT COUNT(rendeles_id) AS ennyi
FROM rendeles_adatok 
WHERE datum >= '$date_start' AND
      datum <= '$date_end' AND
      status = $rendeles_allapot;

If you want a total row in addition to the other data, use WITH ROLLUP:

SELECT datum, COUNT(rendeles_id) AS ennyi
FROM rendeles_adatok 
WHERE datum >= '$date_start' AND
      datum <= '$date_end' AND
      status = $rendeles_allapot
GROUP BY datum WITH ROLLUP;

Upvotes: 1

Related Questions