MySQL筆記之數學函數詳解。本站提示廣大學習愛好者:(MySQL筆記之數學函數詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是MySQL筆記之數學函數詳解正文
相對值函數ABS(x)和圓周率函數PI()
mysql> SELECT ABS(0.5), ABS(-0.5), PI();
+----------+-----------+----------+
| ABS(0.5) | ABS(-0.5) | PI() |
+----------+-----------+----------+
| 0.5 | 0.5 | 3.141593 |
+----------+-----------+----------+
row in set (0.00 sec)
平方根函數SQRT(x)和求余函數MOD(x,y)
mysql> SELECT SQRT(16), SQRT(3), MOD(13,4);
+----------+--------------------+-----------+
| SQRT(16) | SQRT(3) | MOD(13,4) |
+----------+--------------------+-----------+
| 4 | 1.7320508075688772 | 1 |
+----------+--------------------+-----------+
row in set (0.00 sec)
取整函數CEIL(x)、CEILING(x)和FLOOR(x)
mysql> SELECT CEIL(2.3), CEIL(-2.3), CEILING(2.3), CEILING(-2.3);
+-----------+------------+--------------+---------------+
| CEIL(2.3) | CEIL(-2.3) | CEILING(2.3) | CEILING(-2.3) |
+-----------+------------+--------------+---------------+
| 3 | -2 | 3 | -2 |
+-----------+------------+--------------+---------------+
row in set (0.00 sec)
mysql> SELECT FLOOR(2.3), FLOOR(-2.3);
+------------+-------------+
| FLOOR(2.3) | FLOOR(-2.3) |
+------------+-------------+
| 2 | -3 |
+------------+-------------+
row in set (0.00 sec)
CEIL(x)和CEILING(x)前往年夜於或等於x的最小整數
FLOOR(x)前往小於或等於x的最年夜整數
隨機數函數RAND()和RAND(x)
mysql> SELECT RAND(), RAND(2), RAND(2);
+--------------------+--------------------+--------------------+
| RAND() | RAND(2) | RAND(2) |
+--------------------+--------------------+--------------------+
| 0.8269294489425881 | 0.6555866465490187 | 0.6555866465490187 |
+--------------------+--------------------+--------------------+
row in set (0.00 sec)
RAND()和RAND(x)這兩個函數喪失前往0~1的隨機數
差別在於,RAND()前往的數是完整隨機的,而RAND(x)在x雷同時前往的值雷同
四捨五入函數ROUND(x)、ROUND(x,y)和TRUNCATE(x,y)
mysql> SELECT ROUND(2.3), ROUND(2.5), ROUND(2.53,1), ROUND(2.55,1);
+------------+------------+---------------+---------------+
| ROUND(2.3) | ROUND(2.5) | ROUND(2.53,1) | ROUND(2.55,1) |
+------------+------------+---------------+---------------+
| 2 | 3 | 2.5 | 2.6 |
+------------+------------+---------------+---------------+
row in set (0.00 sec)
ROUND(x)前往離x比來的整數,也就是對x停止四捨五入處置
ROUND(x,y)前往x保存到小數點後y位的值,在截取時停止四捨五入處置
mysql> SELECT TRUNCATE(2.53,1), TRUNCATE(2.55,1);
+------------------+------------------+
| TRUNCATE(2.53,1) | TRUNCATE(2.55,1) |
+------------------+------------------+
| 2.5 | 2.5 |
+------------------+------------------+
row in set (0.00 sec)
TRUNCATE(x,y)前往x保存到小數點後y位的值,不停止四捨五入操作
符號函數SIGN(x)
mysql> SELECT SIGN(-2), SIGN(0), SIGN(2);
+----------+---------+---------+
| SIGN(-2) | SIGN(0) | SIGN(2) |
+----------+---------+---------+
| -1 | 0 | 1 |
+----------+---------+---------+
row in set (0.00 sec)
SIGN(x)前往x的符號,-1為正數,0不變,1為整數
冪運算函數POW(x,y)、POWER(x,y)
mysql> SELECT POW(3,2), POWER(3,2);
+----------+------------+
| POW(3,2) | POWER(3,2) |
+----------+------------+
| 9 | 9 |
+----------+------------+
row in set (0.00 sec)