MySQL 查询将两行的值相乘并将结果相加
mysqlmysqli database更新于 2025/11/6 19:24:17
为此,使用聚合函数 SUM()。在此方法中,将行值相乘。让我们首先创建一个表 −
mysql> create table DemoTable( ProductQuantity int, ProductPrice int ); Query OK, 0 rows affected (0.48 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable values(10,9); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(6,20); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(7,100); Query OK, 1 row affected (0.08 sec)
使用 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)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

