MySQL 如何计算MySQL产品表中产品的总价值?

MySQL 如何计算MySQL产品表中产品的总价值?

首先让我们创建一个表 –

mysql> create table DemoTable 
     (
     ProductId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
     ProductQuantity int,
     ProductPrice int 
     ); 
Query OK,0 rows affected(0.19 sec)

使用insert命令在表中插入一些记录 –

mysql> insert into DemoTable(ProductQuantity,ProductPrice) values(10,100); 
Query OK,1 row affected(0.06 sec) 

mysql> insert into DemoTable(ProductQuantity,ProductPrice) values(5,11); 
Query OK,1 row affected(0.04 sec) 

mysql> insert into DemoTable(ProductQuantity,ProductPrice) values(3,140); 
Query OK,1 row affected(0.05 sec) 

mysql> insert into DemoTable(ProductQuantity,ProductPrice) values(2,450); 
Query OK,1 row affected(0.15 sec)

使用select语句显示表中的所有记录 –

mysql> select *from DemoTable;

这将产生以下输出 –

+-----------+-----------------+--------------+ 
| ProductId | ProductQuantity | ProductPrice |
+-----------+-----------------+--------------+
| 1         | 10              | 100          |
| 2         | 5               | 11           |
| 3         | 3               | 140          |
| 4         | 2               | 450          |
+-----------+-----------------+--------------+
4 rows in set (0.00 sec)

以下是计算产品总价值的查询 –

mysql> select sum(ProductQuantity*ProductPrice) as Total_of_Products from DemoTable;

这将产生以下输出 –

+-------------------+
| Total_of_Products |
+-------------------+
| 2375              |
+-------------------+
1 row in set (0.00 sec)

阅读更多:MySQL 教程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程