首页 >数据库 >mysql教程 >MySQL数值运算符和函数

MySQL数值运算符和函数

黄舟
黄舟原创
2017-02-27 13:22:081537浏览

对于普通加减法运算,此处不予详细讲解,如下例:

mysql> SELECT 3+4;
+-----+
| 3+4 |
+-----+
|   7 |
+-----+
1 row in set (0.03 sec)


CEIL和FLOOR

CEIL为向上取整,只要有小数位,不管是几,直接入一位。例如:

mysql> SELECT CEIL(3.01);
+------------+
| CEIL(3.01) |
+------------+
|          4 |
+------------+
1 row in set (0.02 sec)


FLOOR与此

相反

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


p和MOD

p为取整,MOD为取余

mysql> SELECT 3/4;
+--------+
| 3/4    |
+--------+
| 0.7500 |
+--------+
1 row in set (0.00 sec)

mysql> SELECT 3 p 4;
+---------+
| 3 p 4 |
+---------+
|       0 |
+---------+
1 row in set (0.00 sec)

mysql> SELECT 3 MOD 4;
+---------+
| 3 MOD 4 |
+---------+
|       3 |
+---------+
1 row in set (0.00 sec)

注:MOD可以换成%

POWER幂运算

例如3的2次方

mysql> SELECT POWER(3,2);
+------------+
| POWER(3,2) |
+------------+
|          9 |
+------------+
1 row in set (0.14 sec)

ROUND四舍五入

例如3.652保留小数点后两位。

mysql> SELECT ROUND(3.652,2);
+----------------+
| ROUND(3.652,2) |
+----------------+
|           3.65 |
+----------------+
1 row in set (0.00 sec)

保留小数点后一位:

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


TRUNCATE

直接去掉某几位

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 |
+---------------------+
1 row in set (0.00 sec)

 以上就是MySQL数值运算符和函数的内容,更多相关内容请关注PHP中文网(www.php.cn)!



声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn