让我们首先创建一个表-
mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Number int -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(Number) values(10); mysql> insert into DemoTable(Number) values(20); mysql> insert into DemoTable(Number) values(30); mysql> insert into DemoTable(Number) values(40);
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
输出结果
这将产生以下输出-
+----+--------+ | Id | Number | +----+--------+ | 1 | 10 | | 2 | 20 | | 3 | 30 | | 4 | 40 | +----+--------+ 4 rows in set (0.00 sec)
这是选择一个整数并使用MySQL除法的查询-
mysql> select Id,Number,(Number/2) AS DivideBy2 from DemoTable;
这将产生以下输出。除法结果以十进制值表示-
+----+--------+-----------+ | Id | Number | DivideBy2 | +----+--------+-----------+ | 1 | 10 | 5.0000 | | 2 | 20 | 10.0000 | | 3 | 30 | 15.0000 | | 4 | 40 | 20.0000 | +----+--------+-----------+ 4 rows in set (0.00 sec)
如果您不想要十进制值,请使用以下查询并转换为整数-
mysql> select Id,Number,cast((Number/2) AS UNSIGNED) AS DivideBy2 from DemoTable;
这将产生以下输出-
+----+--------+-----------+ | Id | Number | DivideBy2 | +----+--------+-----------+ | 1 | 10 | 5 | | 2 | 20 | 10 | | 3 | 30 | 15 | | 4 | 40 | 20 | +----+--------+-----------+ 4 rows in set (0.00 sec)