程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> MySQL數學函數簡明總結,mysql數學函數

MySQL數學函數簡明總結,mysql數學函數

編輯:MySQL綜合教程

MySQL數學函數簡明總結,mysql數學函數


1. ABS(x): 返回x的絕對值
復制代碼 代碼如下:
mysql> select ABS(1), ABS(-1), ABS(0);
+--------+---------+--------+
| ABS(1) | ABS(-1) | ABS(0) |
+--------+---------+--------+
|      1 |       1 |      0 |
+--------+---------+--------+

2. PI(): 返回圓周率
復制代碼 代碼如下:
mysql> select PI();
+----------+
| PI()     |
+----------+
| 3.141593 |
+----------+

3. SQRT(x): 返回x的平方根,要求(x為非負數,返回NULL)
復制代碼 代碼如下:
mysql> select SQRT(49), SQRT(0), SQRT(-49);
+----------+---------+-----------+
| SQRT(49) | SQRT(0) | SQRT(-49) |
+----------+---------+-----------+
|        7 |       0 |      NULL |
+----------+---------+-----------+

4. MOD(x,y): 求余函數,返回x被y除後的余數;對於帶有小數部分的數據值也起作用,它返回除法運算後的精確余數。
復制代碼 代碼如下:
mysql> select MOD(31,8), MOD(21,-8), MOD(-7,2), MOD(-7,-2), MOD(45.5,6);
+-----------+------------+-----------+------------+-------------+
| MOD(31,8) | MOD(21,-8) | MOD(-7,2) | MOD(-7,-2) | MOD(45.5,6) |
+-----------+------------+-----------+------------+-------------+
|         7 |          5 |        -1 |         -1 |         3.5 |
+-----------+------------+-----------+------------+-------------+

5. CEIL(X): 返回不小X的最小整數值,返回值轉為一個BIGINT.
復制代碼 代碼如下:
mysql> select CEIL(-3.35), CEIL(3.35);
+-------------+------------+
| CEIL(-3.35) | CEIL(3.35) |
+-------------+------------+
|          -3 |          4 |
+-------------+------------+

6. CEILING(X): 同CEIL(X)
復制代碼 代碼如下:
mysql> select CEILING(-3.35), CEILING(3.35);
+----------------+---------------+
| CEILING(-3.35) | CEILING(3.35) |
+----------------+---------------+
|             -3 |             4 |
+----------------+---------------+

7. FLOOR(X):返回不大於X的最大整數值,返回值轉為一個BIGINT.
復制代碼 代碼如下:
mysql> select FLOOR(-3.35), FLOOR(3.35);
+--------------+-------------+
| FLOOR(-3.35) | FLOOR(3.35) |
+--------------+-------------+
|           -4 |           3 |
+--------------+-------------+

8. RAND()和RAND(X)

RAND(X) 返回一個隨機浮點值,范圍在0~1之間,X為整數,它被稱作種子值,用來產生重復序列。即當X值相同時,產生的隨機數也相同;
復制代碼 代碼如下:
mysql> select RAND(10), RAND(10), RAND(2), RAND(-2);
+--------------------+--------------------+--------------------+--------------------+
| RAND(10)           | RAND(10)           | RAND(2)            | RAND(-2)           |
+--------------------+--------------------+--------------------+--------------------+
| 0.6570515219653505 | 0.6570515219653505 | 0.6555866465490187 | 0.6548542125661431 |
+--------------------+--------------------+--------------------+--------------------+

RAND(): 不帶參數的RAND()每次產生不同0~1之間的隨機數
復制代碼 代碼如下:
mysql> SELECT RAND(), RAND(), RAND();
+--------------------+--------------------+---------------------+
| RAND()             | RAND()             | RAND()              |
+--------------------+--------------------+---------------------+
| 0.6931893636409094 | 0.5147262984092592 | 0.49406343185721285 |
+--------------------+--------------------+---------------------+

9. ROUND(X)和ROUND(X,Y): 四捨五入函數,對X值按照Y進行四捨五入,Y可以省略,默認值為0;若Y不為0,則保留小數點後面指定Y位。

復制代碼 代碼如下:mysql> select ROUND(-1.14), ROUND(-1.9), ROUND(1.14), ROUND(1.9);
+--------------+-------------+-------------+------------+
| ROUND(-1.14) | ROUND(-1.9) | ROUND(1.14) | ROUND(1.9) |
+--------------+-------------+-------------+------------+
|           -1 |          -2 |           1 |          2 |
+--------------+-------------+-------------+------------+

mysql> select ROUND(1.38,1), ROUND(1.38,0), ROUND(232.38,-1), ROUND(232.38,-2);
+---------------+---------------+------------------+------------------+
| ROUND(1.38,1) | ROUND(1.38,0) | ROUND(232.38,-1) | ROUND(232.38,-2) |
+---------------+---------------+------------------+------------------+
|           1.4 |             1 |              230 |              200 |
+---------------+---------------+------------------+------------------+

10. TRUNCATE(X,Y): 與ROUND(X,Y)功能類似,但不進行四捨五入,只進行截取。

復制代碼 代碼如下:
mysql> select TRUNCATE(1.33,1), TRUNCATE(1.99,1), TRUNCATE(1.99,0), TRUNCATE(19.99,-1);
+------------------+------------------+------------------+--------------------+
| TRUNCATE(1.33,1) | TRUNCATE(1.99,1) | TRUNCATE(1.99,0) | TRUNCATE(19.99,-1) |
+------------------+------------------+------------------+--------------------+
|              1.3 |              1.9 |                1 |                 10 |
+------------------+------------------+------------------+--------------------+

11. SIGN(X): 返回參數X的符號,X的值為負、零或正數時返回結果依次為-1,0或1

復制代碼 代碼如下:mysql> select SIGN(-21), SIGN(-0),SIGN(0), SIGN(0.0), SIGN(21);
+-----------+----------+---------+-----------+----------+
| SIGN(-21) | SIGN(-0) | SIGN(0) | SIGN(0.0) | SIGN(21) |
+-----------+----------+---------+-----------+----------+
|        -1 |        0 |       0 |         0 |        1 |
+-----------+----------+---------+-----------+----------+

12. POW(X,Y), POWER(X,Y)和EXP(X)

POW(X,Y)與POWER(X,Y)功能相同,用於返回X的Y次乘方的結果值
復制代碼 代碼如下:mysql> select pow(2,2), pow(2,-2), pow(-2,2), pow(-2,-2);
+----------+-----------+-----------+------------+
| pow(2,2) | pow(2,-2) | pow(-2,2) | pow(-2,-2) |
+----------+-----------+-----------+------------+
|        4 |      0.25 |         4 |       0.25 |
+----------+-----------+-----------+------------+

mysql> select power(2,2), power(2,-2), power(-2,2), power(-2,-2);
+------------+-------------+-------------+--------------+
| power(2,2) | power(2,-2) | power(-2,2) | power(-2,-2) |
+------------+-------------+-------------+--------------+
|          4 |        0.25 |           4 |         0.25 |
+------------+-------------+-------------+--------------+

EXP(X): 返回e的X乘方後的值:
復制代碼 代碼如下:
mysql> select EXP(3), EXP(0), EXP(-3);
+-------------------+--------+---------------------+
| EXP(3)            | EXP(0) | EXP(-3)             |
+-------------------+--------+---------------------+
| 20.08553692318767 |      1 | 0.04978706836786393 |
+-------------------+--------+---------------------+

13. LOG(X)和LOG10(X): 對數運算函數(X必須為正數),LOG(X)-返回X的自然對數(X相對於基數e的對數) LOG10(X)-返回x的基數為10的對數:
復制代碼 代碼如下:
mysql> select LOG(-3), LOG(0), LOG(3), LOG10(-100), LOG10(0), LOG10(100);
+---------+--------+--------------------+-------------+----------+------------+
| LOG(-3) | LOG(0) | LOG(3)             | LOG10(-100) | LOG10(0) | LOG10(100) |
+---------+--------+--------------------+-------------+----------+------------+
|    NULL |   NULL | 1.0986122886681098 |        NULL |     NULL |          2 |
+---------+--------+--------------------+-------------+----------+------------+

14. RADIANS(X) 和 DEGREES(X): 角度與弧度轉換函數
復制代碼 代碼如下:
mysql> select RADIANS(90), RADIANS(180), DEGREES(PI()), DEGREES(PI()/2);
+--------------------+-------------------+---------------+-----------------+
| RADIANS(90)        | RADIANS(180)      | DEGREES(PI()) | DEGREES(PI()/2) |
+--------------------+-------------------+---------------+-----------------+
| 1.5707963267948966 | 3.141592653589793 |           180 |              90 |
+--------------------+-------------------+---------------+-----------------+

15. SIN(X), ASIN(X), COS(X), ACOS(X), TAN(X), ATAN(X), COT(X)
SIN(X): 正弦函數,其中X為弧度值
ASIN(X): 反正弦函數 其中X必須在-1到1之間

COS(X): 余弦函數,其中X為弧度值
ACOS(X): 反余弦函數 其中X必須在-1到1之間
TAN(X): 正切函數,其中X為弧度值
ATAN(X): 反正切函數,ATAN(X)與TAN(X)互為反函數

COT(X): 余切函數,函數COT和TAN互為倒函數
復制代碼 代碼如下:
mysql> select SIGN(PI()/2),ASIN(1),COS(PI()), ACOS(-1), TAN(PI()/4), ATAN(1), COT(0.5);
+--------------+--------------------+-----------+-------------------+--------------------+--------------------+-------------------+
| SIGN(PI()/2) | ASIN(1)            | COS(PI()) | ACOS(-1)          | TAN(PI()/4)        | ATAN(1)            | COT(0.5)          |
+--------------+--------------------+-----------+-------------------+--------------------+--------------------+-------------------+
|            1 | 1.5707963267948966 |        -1 | 3.141592653589793 | 0.9999999999999999 | 0.7853981633974483 | 1.830487721712452 |
+--------------+--------------------+-----------+-------------------+--------------------+--------------------+-------------------+


mysql統計函數有什

count() 統計記錄條數,如 select count(*) from stu;
sum() 統計記錄字段的和,如select sum(salary) from emp;
avg() 統計記錄字段的平均值,如select avg(salary) from emp;
max() 查詢字段中的最大值,如select max(age) from stu;
min() 查詢字段中的最小值,如select min(age) from stu;
 

寫了一個mysql的函數,但是運行出錯,哪位幫我看看很急

由於沒有你的表結構, 我另外創建了一個表
CREATE TABLE test_tree ( test_id INT NOT NULL, pid INT, test_val VARCHAR(10), PRIMARY KEY (test_id));INSERT INTO test_tree VALUES(1, NULL, '.NET');INSERT INTO test_tree VALUES(2, 1, 'C#');INSERT INTO test_tree VALUES(3, 1, 'J#');INSERT INTO test_tree VALUES(4, 1, 'ASP.NET');INSERT INTO test_tree VALUES(5, 1, 'VB.NET');INSERT INTO test_tree VALUES(6, NULL, 'J2EE');INSERT INTO test_tree VALUES(7, 6, 'EJB');INSERT INTO test_tree VALUES(8, 6, 'Servlet');INSERT INTO test_tree VALUES(9, 6, 'JSP');INSERT INTO test_tree VALUES(10, NULL, 'Database');INSERT INTO test_tree VALUES(11, 10, 'DB2');INSERT INTO test_tree VALUES(12, 10, 'MySQL');INSERT INTO test_tree VALUES(13, 10, 'Oracle');INSERT INTO test_tree VALUES(14, 10, 'SQL Server');INSERT INTO test_tree VALUES(15, 13, 'PL/SQL');INSERT INTO test_tree VALUES(16, 15, 'Function');INSERT INTO test_tree VALUES(17, 15, 'Procedure');INSERT INTO test_tree VALUES(18, 15, 'Package');INSERT INTO test_tree VALUES(19, 15, 'Cursor');INSERT INTO test_tree VALUES(20, 14, 'T-SQL');

修改後的函數代碼
DELIMITER //create function groupLst(rootid varchar(25)) returns varchar(1000)begin declare idStr VARCHAR(1000); declare chID VARCHAR(1000); set max_sp_recursion_depth=225; set idSt......余下全文>>
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved