Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mysql对每行求和_Mysql_Sum_Row - Fatal编程技术网

mysql对每行求和

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

查询以获得如下结果

表价( 项目int, 金额int, 数量int, )


结果

+-------+-------+----------+-----------+-------+
| 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;