如何在MySQL的新列中获得两列之间的差异?
让我们首先创建一个带有列的表,我们将为其计算新列中的差-
mysql> create table DemoTable(
Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
LowValue int,
HighValue int
);
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(LowValue,HighValue) values(100,200);mysql> insert into DemoTable(LowValue,HighValue) values(300,700);
mysql> insert into DemoTable(LowValue,HighValue) values(1000,2000);
以下是使用select语句显示表中所有记录的查询-
mysql> select *from DemoTable;
这将产生以下输出-
+----+----------+-----------+| Id | LowValue | HighValue |
+----+----------+-----------+
| 1 | 100 | 200 |
| 2 | 300 | 700 |
| 3 | 1000 | 2000 |
+----+----------+-----------+
3 rows in set (0.00 sec)
这是获取2列之间的差的查询-
mysql> select tbl.Id,tbl.LowValue,tbl.HighValue,HighValue-LowValue AS DifferenceBetweenTwoColumns from DemoTable tbl;
这将产生以下输出-
+----+----------+-----------+-----------------------------+| Id | LowValue | HighValue | DifferenceBetweenTwoColumns |
+----+----------+-----------+-----------------------------+
| 1 | 100 | 200 | 100 |
| 2 | 300 | 700 | 400 |
| 3 | 1000 | 2000 | 1000 |
+----+----------+-----------+-----------------------------+
3 rows in set (0.00 sec)
以上是 如何在MySQL的新列中获得两列之间的差异? 的全部内容, 来源链接: utcz.com/z/326884.html