MySQL 數字函式大全

神諭丶發表於2015-10-09
以下內容基於MySQL 5.6及更高,大部分函式5.5也基本適用,更低版本請參考對應版本手冊,其內容整理自官方。

mysql常用函式被分為五類,分別為:

①Numeric:數字函式;

②String:字串函式;
http://blog.itpub.net/29773961/viewspace-1813545/

③Date and time:日期和時間;
http://blog.itpub.net/29773961/viewspace-1808967/

④Control Flow:基於一個表示式的結果集選擇不同的值(控制流函式);
http://blog.itpub.net/29773961/viewspace-1813557/

⑤Aggregate:基於一列的多個值返回單一值(聚合函式);
http://blog.itpub.net/29773961/viewspace-1813589/

【Numeric
】:
ABS()返回絕對值
  1. mysql> SELECT ABS(-22), ABS(0), ABS(33);
  2. +----------+--------+---------+
  3. | ABS(-22) | ABS(0) | ABS(33) |
  4. +----------+--------+---------+
  5. | 22       | 0      | 33      |
  6. +----------+--------+---------+
  7. 1 row in set (0.00 sec)


ACOS():返回arccos


ASIN():返回arcsin


ATAN():返回arctan


CEIL()與CEILING():向上取整
  1. mysql> SELECT CEIL(-1.2), CEIL(1.8), CEILING(0.4), CEILING(0.8);
  2. +------------+-----------+--------------+--------------+
  3. | CEIL(-1.2) | CEIL(1.8) | CEILING(0.4) | CEILING(0.8) |
  4. +------------+-----------+--------------+--------------+
  5. | -1         | 2         | 1            | 1            |
  6. +------------+-----------+--------------+--------------+
  7. 1 row in set (0.00 sec)


CONV():不同進位制的轉換
  1. mysql> SELECT CONV('a',16,2) 將16進位制數a轉換為2進位制,
  2.     -> CONV('6E',18,8),
  3.     -> CONV(10+'10'+'10'+0xa,10,10);
  4. +---------------------------------+-----------------+------------------------------+
  5. | 將16進位制數a轉換為2進位制            | CONV('6E',18,8) | CONV(10+'10'+'10'+0xa,10,10) |
  6. +---------------------------------+-----------------+------------------------------+
  7. | 1010                            | 172             | 40                           |
  8. +---------------------------------+-----------------+------------------------------+
  9. 1 row in set (0.00 sec)


COS():返回餘弦值


COT():返回餘切值


CRC32():返回迴圈冗餘校驗值


DEGREES():將弧度變為角度
  1. mysql> SELECT DEGREES(PI());
  2. +---------------+
  3. | DEGREES(PI()) |
  4. +---------------+
  5. | 180            
  6. +---------------+
  7. 1 row in set (0.01 sec)


EXP():返回以e為底的某次方的值


FLOOR():返回最大不超過表示式的整數
  1. mysql> SELECT FLOOR(1.34), FLOOR(-0.1112), FLOOR(2.56);
  2. +-------------+----------------+-------------+
  3. | FLOOR(1.34) | FLOOR(-0.1112) | FLOOR(2.56) |
  4. +-------------+----------------+-------------+
  5. | 1           | -1             | 2           |
  6. +-------------+----------------+-------------+
  7. 1 row in set (0.00 sec)


LN():返回log以e為底,某指的對數


LOG10():返回log以10為底,某值的對數


LOG2():返回log以2為底,某值的對數


LOG(expr1,expr2):返回以expr1為底數,expr2值的對數


MOD(expr1,expr2):返回expr1模expr2的值。


PI():返回π的值


POW(expr1,expr2)與POWER(expr1,expr2):返回expr1的expr2次方的值
  1. mysql> SELECT POW(1,99), POWER(1,99),
  2.     -> POW(2,10), POWER(-2,5);
  3. +-----------+-------------+-----------+-------------+
  4. | POW(1,99) | POWER(1,99) | POW(2,10) | POWER(-2,5) |
  5. +-----------+-------------+-----------+-------------+
  6. | 1         | 1           | 1024      | -32         |
  7. +-----------+-------------+-----------+-------------+
  8. 1 row in set (0.02 sec)


RADIANS():將角度變為弧度
  1. mysql> SELECT RADIANS(180);
  2. +-------------------+
  3. | RADIANS(180)      |
  4. +-------------------+
  5. | 3.141592653589793 |
  6. +-------------------+
  7. 1 row in set (0.00 sec)


RAND():返回一個浮點隨機數(0~1開區間)
  1. mysql> SELECT RAND();
  2. +--------------------+
  3. | rand()             |
  4. +--------------------+
  5. | 0.7237686484342148 |
  6. +--------------------+
  7. 1 row in set (0.00 sec)


ROUND():將某值四捨五入
  1. mysql> SELECT ROUND(PI()), ROUND(-2.333), ROUND(-2.7666), ROUND(3.611);
  2. +-------------+---------------+----------------+--------------+
  3. | ROUND(PI()) | ROUND(-2.333) | ROUND(-2.7666) | ROUND(3.611) |
  4. +-------------+---------------+----------------+--------------+
  5. | 3           | -2            | -3             | 4            |
  6. +-------------+---------------+----------------+--------------+
  7. 1 row in set (0.00 sec)


SIGN():返回正負(-1=負值,0=零,1=正值)
  1. mysql> SELECT SIGN(-3.14), SIGN(0), SIGN(22.1)
  2.     -> ;
  3. +-------------+---------+------------+
  4. | SIGN(-3.14) | SIGN(0) | SIGN(22.1) |
  5. +-------------+---------+------------+
  6. | -1          | 0       | 1          |
  7. +-------------+---------+------------+
  8. 1 row in set (0.00 sec)


SIN():返回正弦值


SQRT():將某值開平方(當然用POW(expr1,1/2)也可以)
  1. mysql> SELECT SQRT(1024),SQRT(9);
  2. +------------+---------+
  3. | SQRT(1024) | SQRT(9) |
  4. +------------+---------+
  5. | 32         | 3       |
  6. +------------+---------+
  7. 1 row in set (0.00 sec)


TAN():返回正切值


TRUNCATE(expr1,expr2):將expr1返回以保留expr2個小數點
  1. mysql> SELECT TRUNCATE(2.111111,3);
  2. +----------------------+
  3. | TRUNCATE(2.111111,3) |
  4. +----------------------+
  5. | 2.111                |
  6. +----------------------+
  7. 1 row in set (0.00 sec)


作者公眾號(持續更新)

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29773961/viewspace-1813556/,如需轉載,請註明出處,否則將追究法律責任。

相關文章