MySQL查询从具有成本和数量的列值计算总金额?
让我们首先创建一个表-
mysql> create table DemoTable ( Cost int, Quantity int );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values(65,2); mysql> insert into DemoTable values(290,4); mysql> insert into DemoTable values(40,3);
使用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)