mysql对每行求和
查询以获得如下结果 表价( 项目int, 金额int, 数量int, )mysql对每行求和,mysql,sum,row,Mysql,Sum,Row,查询以获得如下结果 表价( 项目int, 金额int, 数量int, ) 结果 +-------+-------+----------+-----------+-------+ | item | price | quantity | sub total | total | +-------+-------+----------+-----------+-------+ | box 1 | 1000 | 4 | 4000 | 16000 | | box 2 | 20
结果
+-------+-------+----------+-----------+-------+
| item | price | quantity | sub total | total |
+-------+-------+----------+-----------+-------+
| box 1 | 1000 | 4 | 4000 | 16000 |
| box 2 | 2000 | 1 | 2000 | 18000 |
| box 3 | 3000 | 6 | 18000 | 36000 |
+-------+-------+----------+-----------+-------+
如果您的mysql版本低于8.0,您可以在下面尝试
select
item,price,quantity,price*quantity as total,
@totalall:= @totalall + price*quantity as TotalAll
from price, (Select @totalall:= 0) as totalall;
或者,如果您的mysql vVersion 8.0+可以在下面进行尝试-
SELECT
item,price,quantity,price*quantity as total,
SUM(price*quantity) OVER(ORDER BY item) AS TotalAll
FROM price;
选择项目、金额(@sum:=@sum+(项目*金额))作为交叉联接的数量(选择@sum:=0)参数请应用格式。你试过什么?你想要什么结果?你的mysql版本是什么?
SELECT
item,price,quantity,price*quantity as total,
SUM(price*quantity) OVER(ORDER BY item) AS TotalAll
FROM price;