如何在MySQL中将一个值进行CAST并与另一个值执行除法?
首先,使用CAST()函数进行类型转换。让我们创建一个表-
mysql> create table DemoTable1352
-> (
-> Value1 int,
-> Value2 int
-> );
Query OK, 0 rows affected (0.54 sec)
使用insert命令将一些记录插入到该表中-
mysql> insert into DemoTable1352 values(10,30);
Query OK, 1 row affected (0.42 sec)
mysql> insert into DemoTable1352 values(40,60);
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable1352 values(110,130);
Query OK, 1 row affected (0.18 sec)
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1352;
这将产生以下输出:
+--------+--------+
| Value1 | Value2 |
+--------+--------+
| 10 | 30 |
| 40 | 60 |
| 110 | 130 |
+--------+--------+
3 rows in set (0.00 sec)
以下是将一个值进行类型转换并执行除法的查询-
mysql> select cast(Value1 as DECIMAL(18,14)) / Value2 from DemoTable1352;
这将产生以下输出-
+-----------------------------------------+
| cast(Value1 as DECIMAL(18,14)) / Value2 |
+-----------------------------------------+
| 0.33333333333333 |
| 0.66666666666667 |
| 0.84615384615385 |
+-----------------------------------------+
3 rows in set (0.00 sec)
阅读更多:MySQL 教程