为此,请使用聚合函数SUM()。在此方法中,将行值相乘。让我们首先创建一个表-mysql> create table DemoTable(
ProductQuantity int,
ProductPrice int
);
使用插入命令在表中插入一些记录-mysql> insert into DemoTable values(10,9);
mysql> insert into DemoTable values(6,20);
mysql> insert into DemoTable values(7,100);
使用select语句显示表中的所有记录-mysql> select *from DemoTable;
这将产生以下输出-+-----------------+--------------+
| ProductQuantity | ProductPrice |
+-----------------+--------------+
| 10 | 9 |
| 6 | 20 |
| 7 | 100 |
+-----------------+--------------+
3 rows in set (0.00 sec)
以下是将两行的值相乘并将结果相加的查询-mysql> select SUM(ProductQuantity*ProductPrice) AS Total_Value from DemoTable;
这将产生以下输出-+-------------+
| Total_Value |
+-------------+
| 910 |
+-------------+
1 row in set (0.00 sec)