mysql数值运算符和函数

Posted 浅色夏沫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql数值运算符和函数相关的知识,希望对你有一定的参考价值。

 

mysql> SELECT CEIL(3.01);  # 进1取整
+------------+
| CEIL(3.01) |
+------------+
| 4 |
+------------+
1 row in set (0.00 sec)

mysql> SELECT FLOOR(3.99);  # 舍1取整
+-------------+
| FLOOR(3.99) |
+-------------+
| 3 |
+-------------+
1 row in set (0.00 sec)

mysql> SELECT 3 / 4; #真正的除法

+--------+
| 3 / 4 |
+--------+
| 0.7500 |
+--------+
1 row in set (0.00 sec)

mysql>
mysql> SELECT 3 DIV 4;  #整数除法
+---------+
| 3 DIV 4 |
+---------+
| 0 |
+---------+
1 row in set (0.00 sec)

mysql> SELECT 5 % 3;  #取余数
+-------+
| 5 % 3 |
+-------+
| 2 |
+-------+
1 row in set (0.00 sec)

mysql> SELECT 5 MOD 3;    #取余数
+---------+
| 5 MOD 3 |
+---------+
| 2 |
+---------+
1 row in set (0.01 sec)

mysql> SELECT POWER(3,3);  #幂运算
+------------+
| POWER(3,3) |
+------------+
| 27 |
+------------+
1 row in set (0.05 sec)

mysql> SELECT ROUND(3.654, 2);  #四舍五入
+-----------------+
| ROUND(3.654, 2) |
+-----------------+
| 3.65 |
+-----------------+
1 row in set (0.00 sec)

mysql> SELECT ROUND(3.654, 1);
+-----------------+
| ROUND(3.654, 1) |
+-----------------+
| 3.7 |
+-----------------+
1 row in set (0.00 sec)

mysql> SELECT TRUNCATE(125.89, 2);  # 数字截取
+---------------------+
| TRUNCATE(125.89, 2) |
+---------------------+
| 125.89 |
+---------------------+
1 row in set (0.00 sec)

mysql> SELECT TRUNCATE(125.89, 1);
+---------------------+
| TRUNCATE(125.89, 1) |
+---------------------+
| 125.8 |
+---------------------+
1 row in set (0.00 sec)

mysql> SELECT TRUNCATE(125.89, 0);
+---------------------+
| TRUNCATE(125.89, 0) |
+---------------------+
| 125 |
+---------------------+
1 row in set (0.00 sec)

mysql> SELECT TRUNCATE(125.89, -1);  #很少用到
+----------------------+
| TRUNCATE(125.89, -1) |
+----------------------+
| 120 |
+----------------------+

以上是关于mysql数值运算符和函数的主要内容,如果未能解决你的问题,请参考以下文章

MySQL学习18:运算符与函数之数值运算符和函数比较运算符和函数日期时 间函数

mysql 运算符和函数

MySQL数据类型 - JSON数据类型

前端学数据库之函数

mysql 基本命令-数据类型和运算符

前端学数据库之函数