查询以划分两列的值,并使用MySQL通配符在新列中显示结果?
让我们首先创建一个表-
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Value1 int, Value2 int );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(Value1,Value2) values(100,150); mysql> insert into DemoTable(Value1,Value2) values(500,1000); mysql> insert into DemoTable(Value1,Value2) values(15000,18000);
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+----+--------+--------+ | Id | Value1 | Value2 | +----+--------+--------+ | 1 | 100 | 150 | | 2 | 500 | 1000 | | 3 | 15000 | 18000 | +----+--------+--------+ 3 rows in set (0.00 sec)
现在,让我们从两列中划分值,并使用MySQL通配符在新列中显示结果-
mysql> select *,(Value1/Value2) AS Result from DemoTable;
这将产生以下输出-
+----+--------+--------+--------+ | Id | Value1 | Value2 | Result | +----+--------+--------+--------+ | 1 | 100 | 150 | 0.6667 | | 2 | 500 | 1000 | 0.5000 | | 3 | 15000 | 18000 | 0.8333 | +----+--------+--------+--------+ 3 rows in set (0.00 sec)