天天看點

MySQL 内置數學函數

學習學習mysql内置數學函數,先進入mysql:

C:\Users\Administrator>cd /d D:\MySQL Server 5.5\bin

D:\MySQL Server 5.5\bin>mysql -u root -p
Enter password: ********
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.28 MySQL Community Server (GPL)

Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>      

如直接輸入以下指令,卻出現:

MySQL 内置數學函數

說明沒有設定系統的環境變量。

我們需要把Mysql bin檔案夾的位址添加到windows的“環境變量”/“系統變量”/path 中

如出現

ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061)的錯誤,應該是mysql服務沒有開啟。以管理者身份運作cmd,開啟即可。還不懂就看:

數學函數:

abs(x) 傳回數字的絕對值

floor(x) 傳回不大于x的最大整數

rand() 傳回0至1的随機數

rand(x) 也傳回随機數,不過x作為種子

MySQL 内置數學函數

mysql> select abs(-2),floor(1.5),rand(),rand(),PI();
+---------+------------+--------------------+--------------------+----------+
| abs(-2) | floor(1.5) | rand()             | rand()             | PI()     |
+---------+------------+--------------------+--------------------+----------+
|       2 |          1 | 0.8543179415672253 | 0.6597909654116174 | 3.141593 |
+---------+------------+--------------------+--------------------+----------+      

TRUNCAT用于單純的截取小數。

mysql> select TRUNCATE(5.242354,4);
+----------------------+
| TRUNCATE(5.242354,4) |
+----------------------+
|               5.2423 |
+----------------------+      

round()四舍五入取最近整數。

round(x,y)保留x小數點後y位的值

mysql> select round(5.242354,4);
+-------------------+
| round(5.242354,4) |
+-------------------+
|            5.2424 |
+-------------------+

mysql> select round(1.4),round(1.5),round(1.6);
+------------+------------+------------+
| round(1.4) | round(1.5) | round(1.6) |
+------------+------------+------------+
|          1 |          2 |          2 |
+------------+------------+------------+      

sign(x) 判斷x的正負性。

x>0: 1 

x<0: -1

x=0: 0

mysql> select sign(4),sign(-4),sign(0);
+---------+----------+---------+
| sign(4) | sign(-4) | sign(0) |
+---------+----------+---------+
|       1 |       -1 |       0 |
+---------+----------+---------+      

pow(x,y)  

sqrt(x)  

EXP(x) 傳回e的x乘方後的值

mod(x,y)  傳回 x mod y的值

log(x) 傳回

MySQL 内置數學函數

的值

log10(x)

mysql>  select pow(2,6),sqrt(64),exp(2),mod(65,64),log(64),log10(100);
+----------+----------+------------------+------------+--------------------+------------+
| pow(2,6) | sqrt(64) | exp(2)           | mod(65,64) | log(64)            | log10(100) |
+----------+----------+------------------+------------+--------------------+------------+
|       64 |        8 | 7.38905609893065 |          1 | 4.1588830833596715 |          2 |
+----------+----------+------------------+------------+--------------------+------------+      

和角度相關的函數:

radians(x) 角度轉弧度

degrees(x) 弧度轉角度

mysql> select radians(180),degrees(PI());
+-------------------+---------------+
| radians(180)      | degrees(PI()) |
+-------------------+---------------+
| 3.141592653589793 |           180 |
+-------------------+---------------+      

三角函數相關的函數:

mysql>  select sin(PI()/2),cos(PI()/2.0),acos(0),tan(PI()/4.0),atan(1);
+-------------+-----------------------+--------------------+--------------------+--------------------+
| sin(PI()/2) | cos(PI()/2.0)         | acos(0)            | tan(PI()/4.0)      | atan(1)            |
+-------------+-----------------------+--------------------+--------------------+--------------------+
|           1 | 6.123233995736766e-17 | 1.5707963267948966 | 0.9999999999999999 | 0.7853981633974483 |
+-------------+-----------------------+--------------------+--------------------+--------------------+      

參數都是弧度。