MySQL 查询来计算来自“成本”和“数量”带有列值的总量?


首先,我们创建一个表 -

mysql> create table DemoTable
(
   Cost int,
   Quantity int
);
Query OK, 0 rows affected (0.80 sec)

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

mysql> insert into DemoTable values(65,2);
Query OK, 1 row affected (0.08 sec)
mysql> insert into DemoTable values(290,4);
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable values(40,3);
Query OK, 1 row affected (0.10 sec)

使用 select 语句从表中显示所有记录 -

mysql> select *from DemoTable;

这将产生以下输出 -

+------+----------+
| Cost | Quantity |
+------+----------+
| 65   |        2 |
| 290  |        4 |
| 40   |        3 |
+------+----------+
3 rows in set (0.00 sec)

以下是计算总金额的查询 -

mysql> select Cost,Quantity,Cost*Quantity AS Total_Amount from DemoTable;

这将产生以下输出 -

+------+----------+--------------+
| Cost | Quantity | Total_Amount |
+------+----------+--------------+
|   65 |        2 |          130 |
|  290 |        4 |         1160 |
|   40 |        3 |          120 |
+------+----------+--------------+
3 rows in set (0.02 sec)

更新于: 2019-10-01

885 次访问

开启你的职业生涯生涯

完成课程,获得认证

入门
广告