在表中添加新列,并在MySQL中用同一表的其他两个列的数据填充它?

让我们首先创建一个表-

mysql> create table DemoTable
(
   Price int,
   Quantity int
);

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

mysql> insert into DemoTable values(45,3);
mysql> insert into DemoTable values(90,2);
mysql> insert into DemoTable values(440,1);

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

mysql> select *from DemoTable;

这将产生以下输出-

+-------+----------+
| Price | Quantity |
+-------+----------+
|    45 |        3 |
|    90 |        2 |
|   440 |        1 |
+-------+----------+
3 rows in set (0.00 sec)

以下是向表中添加新列并填充其他2列数据的查询。在这里,我们添加了新列TotalAmount,其中的数据是前两列的值的乘积-

mysql> select Price,Quantity, Price*Quantity AS TotalAmount from DemoTable;

这将产生以下输出-

+-------+----------+-------------+
| Price | Quantity | TotalAmount |
+-------+----------+-------------+
|    45 |        3 |         135 |
|    90 |        2 |         180 |
|   440 |        1 |         440 |
+-------+----------+-------------+
3 rows in set (0.00 sec)
猜你喜欢