select car_type, sum(price*number) as price from vehicul group by car_type..
http://sqlfiddle.com/#!2/3d881/1
you should use sum(field_1*field_2) instead of sum()*sum()
Pregunta
I am trying to do a multiplication operation in MySQL. I have the following table called vehicul:
id car_type car_model number price car_id
1 audi <model1> 2 320 1
2 audi <model2> 4 100 1
3 bmw <model1> 3 240 2
4 bmw <model2> 6 500 2
5 bmw <model3> 1 400 2
I have this command:
SELECT car_type, sum(price) FROM vehicul as v, GROUP BY marca
Which displays the following:
car_type price
audi 420
bmw 1140
But what I want is to display the number * price and the result to be:
car_type price
audi 1040
bmw 4120
Basically.. sum(number*price).. how can I do that? I have tried doing:
SELECT car_type, sum(price*number) FROM vehicul as v, GROUP BY marca
But I am getting:
#1052 - Column 'number' in field list is ambiguous
Solución
select car_type, sum(price*number) as price from vehicul group by car_type..
http://sqlfiddle.com/#!2/3d881/1
you should use sum(field_1*field_2) instead of sum()*sum()
Otros consejos
Your query is totally correct. You may have more than 1 table which contain 2 or more number column. Try:
SELECT car_type, sum(price)*sum(number)
FROM vehicul
GROUP BY marca;
But with the logic you will need this to get total price:
SELECT car_type, sum(price*number)
FROM vehicul
GROUP BY marca;