简体   繁体   中英

How do I add multiple totals in mysql?

I'm trying to find the total amount for each method of payment listed in a table. I have the sum of each row, and now I need to add those sums. The query is:

select payment_method, (deposit + pet_deposit + cleaning_fee + rental_rate) as            
total
from reservations
group by total
order by payment_method;

and this is the result set:

+----------------+---------+
| payment_method | total   |
+----------------+---------+
| Check          |  760.00 |
| Check          |  960.00 |
| Check          |  660.00 |
| MasterCard     |  600.00 |
| MasterCard     | 1160.00 |
| PayPal         |  500.00 |
| Visa           |  560.00 |
| Visa           |  610.00 |
| Visa           |  700.00 |
+----------------+---------+
9 rows in set (0.00 sec)

How would I make it so there's only instance of each payment method, and the total column has the full total?

Just GROUP BY the payment method, like this:

SELECT payment_method, 
     SUM(deposit + pet_deposit + cleaning_fee + rental_rate) 
          AS total
FROM reservations
GROUP BY payment_method
ORDER BY payment_method;

Your result should then look like this (sample data in sums):

+----------------+---------+
| payment_method | total   |
+----------------+---------+
| Check          |  123.45 |
| MasterCard     |  234.56 |
| PayPal         |  345.67 |
| Visa           |  456.78 |
+----------------+---------+

按付款方式而不是总计分组。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM