在表中添加新列,并在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)
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短