绝对值函数
语法格式:ABS(X)
例:查看三个数值的绝对值(负的绝对值为它的正整数,0的绝对值为0,正的绝对值为它本身)。
1. mysql> select abs(2),abs(-32),abs(-0.5); 2. +--------+----------+-----------+ 3. | abs(2) | abs(-32) | abs(-0.5) | 4. +--------+----------+-----------+ 5. | 2 | 32 | 0.5 | 6. +--------+----------+-----------+ 7. 1 row in set (0.01 sec)
求余函数
语法格式:MOD(N,M), N % M, N MOD M
例:查看三个数值,三个方法查看取余。
1. mysql> select mod(10,3),20%3,15 mod 4; 2. +-----------+------+----------+ 3. | mod(10,3) | 20%3 | 15 mod 4 | 4. +-----------+------+----------+ 5. | 1 | 2 | 3 | 6. +-----------+------+----------+ 7. 1 row in set (0.00 sec)
获取整数的函数
语法格式:CEIL(X)、CEILING(x) 、FLOOR(x)
例:ceil和ceiling返回不小于x的最小整数,floor返回不大于x的最大整数。
1. mysql> select ceil(2.1234),ceiling(3.1234),floor(4.1234); 2. +--------------+-----------------+---------------+ 3. | ceil(2.1234) | ceiling(3.1234) | floor(4.1234) | 4. +--------------+-----------------+---------------+ 5. | 3 | 4 | 4 | 6. +--------------+-----------------+---------------+ 7. 1 row in set (0.00 sec)
获取随机数的函数
语法格式:RAND() 、RAND(x)
例1:rand()用于返回一个0~1之间的随机浮点数。
1. mysql> select rand(),rand(); 2. +--------------------+---------------------+ 3. | rand() | rand() | 4. +--------------------+---------------------+ 5. | 0.8802711524816893 | 0.21204793100436026 | 6. +--------------------+---------------------+ 7. 1 row in set (0.00 sec) 8. 例2:rand(x)用于返回一个0~1之间的浮点数,x作为种子值用来产生重复序列。 9. mysql> select rand(2),rand(2),rand(1000000); 10. +--------------------+--------------------+--------------------+ 11. | rand(2) | rand(2) | rand(1000000) | 12. +--------------------+--------------------+--------------------+ 13. | 0.6555866465490187 | 0.6555866465490187 | 0.2644144708890603 | 14. +--------------------+--------------------+--------------------+ 15. 1 row in set (0.00 sec)
四舍五入的函数
语法格式:ROUND(x) 、ROUND(x,y)
例:round(x)四舍五入为整数,round(x,y)四舍五入保留后面两位小数。
1. mysql> select round(3.512),round(4.235,2); 2. +--------------+----------------+ 3. | round(3.512) | round(4.235,2) | 4. +--------------+----------------+ 5. | 4 | 4.24 | 6. +--------------+----------------+ 7. 1 row in set (0.00 sec)
截取数值的函数
语法格式:TRUNCATE(x,y)
例1:截取数值,保留y位。
1. mysql> select truncate(2.567,2),truncate(1.99,1); 2. +-------------------+------------------+ 3. | truncate(2.567,2) | truncate(1.99,1) | 4. +-------------------+------------------+ 5. | 2.56 | 1.9 | 6. +-------------------+------------------+ 7. 1 row in set (0.00 sec)
例2:截取1234为1000,截取1234为1200(负数表明把右侧的数值截取为0,看到下面的操作可以想到,把负数改为-1可以看到多少?结果为1230!)
1. mysql> select truncate(1234,-3),truncate(1234,-2); 2. +-------------------+-------------------+ 3. | truncate(1234,-3) | truncate(1234,-2) | 4. +-------------------+-------------------+ 5. | 1000 | 1200 | 6. +-------------------+-------------------+ 7. 1 row in set (0.00 sec)
符号函数
语法格式:SIGN(x)
例:sign(x)表示任何负数返回-1,0返回0,任何正数返回1。
1. mysql> select sign(-123),sign(0),sign(223); 2. +------------+---------+-----------+ 3. | sign(-123) | sign(0) | sign(223) | 4. +------------+---------+-----------+ 5. | -1 | 0 | 1 | 6. +------------+---------+-----------+ 7. 1 row in set (0.00 sec)
幂运算函数
语法格式:POW(x,y) 、POWER(x,y)
例:两个用处相同,pow为返回2的三次方,power返回3的四次方。
1. mysql> select pow(2,3),power(3,4); 2. +----------+------------+ 3. | pow(2,3) | power(3,4) | 4. +----------+------------+ 5. | 8 | 81 | 6. +----------+------------+ 7. 1 row in set (0.00 sec)