我正在尝试在MySQL中进行乘法运算。 我有下表叫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
我有这个命令:
SELECT car_type, sum(price) FROM vehicul as v, GROUP BY marca
显示以下内容:
car_type price
audi 420
bmw 1140
但我想要的是显示数字*价格和结果:
car_type price
audi 1040
bmw 4120
基本上..总和(数字*价格)..我该怎么办?我试过了:
SELECT car_type, sum(price*number) FROM vehicul as v, GROUP BY marca
但我得到了:
#1052 - Column 'number' in field list is ambiguous
答案 0 :(得分:2)
通过car_type选择car_type,sum(price * number)作为来自vehicul group的价格。
http://sqlfiddle.com/#!2/3d881/1
你应该使用sum(field_1 * field_2)而不是sum()* sum()
答案 1 :(得分:1)
您的查询完全正确。您可能有多个表包含2个或更多数字列。尝试:
SELECT car_type, sum(price)*sum(number)
FROM vehicul
GROUP BY marca;
但是根据逻辑,您需要这样才能获得总价:
SELECT car_type, sum(price*number)
FROM vehicul
GROUP BY marca;