1 数值处理函数

函数 说明
Abs(<number>) 返回一个数的绝对值
Exp(<number>) 返回一个数的指数值,等同于 \(e^{[number]}\)
Mod(<number1>, <number2>) 返回除操作的余数
Pi() 返回圆周率
Rand() 返回一个随机数,范围在 [0,1)
Cos(<number>) 返回一个角度的余弦
Sin(<number>) 返回一个角度的正弦
Tan(<number>) 返回一个角度的正切
Sqrt(<number>) 返回一个数的平方根

下图是三角函数值的表,可以与 MySQL 计算的值对照一下

三角函数值对照表

1.1 Abs(<number>)

1
2
3
4
5
6
mysql> SELECT Abs(-1);
+---------+
| Abs(-1) |
+---------+
|       1 |
+---------+

1.2 Exp(<number>)

1
2
3
4
5
6
mysql> SELECT Exp(1);
+-------------------+
| Exp(1)            |
+-------------------+
| 2.718281828459045 |
+-------------------+

1.3 Mod(<number1>, <number2>)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
mysql> SELECT Mod(3, 2);
+-----------+
| Mod(3, 2) |
+-----------+
|         1 |
+-----------+
# 还可以这样写
mysql> SELECT 3 % 2;
+-------+
| 3 % 2 |
+-------+
|     1 |
+-------+
# 还可以这样写
mysql> SELECT 3 MOD 2;
+---------+
| 3 MOD 2 |
+---------+
|       1 |
+---------+

1.4 Pi()

1
2
3
4
5
6
mysql> SELECT PI();
+----------+
| PI()     |
+----------+
| 3.141593 |
+----------+

1.5 Rand()

1
2
3
4
5
6
mysql> SELECT RAND();
+--------------------+
| RAND()             |
+--------------------+
| 0.7299242734256427 |
+--------------------+

1.6 Cos(<number>)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
mysql> SELECT Cos(0);
+--------+
| Cos(0) |
+--------+
|      1 |
+--------+

mysql> SELECT Cos(PI());
+-----------+
| Cos(PI()) |
+-----------+
|        -1 |
+-----------+

1.7 Sin(<number>)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
mysql> SELECT Sin(0);
+--------+
| Sin(0) |
+--------+
|      0 |
+--------+
# 他这里面 Sin(PI()) 应该等于 0 才对
# 这个数据不对不知道是不是由于 PI() 
# 的小数尾数造成的
mysql> SELECT Sin(PI());
+------------------------+
| Sin(PI())              |
+------------------------+
| 1.2246467991473532e-16 |
+------------------------+

1.8 Tan(<number>)

1
2
3
4
5
6
mysql> SELECT Tan(0);
+--------+
| Tan(0) |
+--------+
|      0 |
+--------+

1.9 Sqrt(<number>)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
mysql> SELECT SQRT(1);
+---------+
| SQRT(1) |
+---------+
|       1 |
+---------+

mysql> SELECT SQRT(2);
+--------------------+
| SQRT(2)            |
+--------------------+
| 1.4142135623730951 |
+--------------------+

mysql> SELECT SQRT(4);
+---------+
| SQRT(4) |
+---------+
|       2 |
+---------+

参考资料

  1. SQL Tutorial