How (MySQL) to multiply columns and then rows of sum?

I have this table:

id | payment_id | quantity | unit cost 1 633 1 750 2 633 1 750 3 632 2 750 4 632 2 750 

What I need:

 payemnt_id | total 633 1500 632 3000 

You can also think of it as a country, and then try to find the total for each country. I was sure there were some lessons like this, but could not be found on STFW.

+4
source share
3 answers

You can simply put the formula (expression) inside the SUM :

 SELECT payment_id, SUM(`unit cost` * quantity) AS total FROM myTable GROUP BY payment_id 
+21
source
 SELECT payment_id, SUM(subtotal) AS total FROM( SELECT payment_id, (unit_cost * quantity) AS subtotal ) AS t1 GROUP BY payment_id 
+2
source
 SELECT payemnt_id, SUM(`unit cost` * quantity) as total FROM Table1 GROUP BY payemnt_id 
+1
source

Source: https://habr.com/ru/post/1492911/


All Articles