1数学函数
- a 绝对值函数
ABS(X):返回X的绝对值;
mysql> select ABS(3),abs(-3);
+--------+---------+
| ABS(3) | abs(-3) |
+--------+---------+
| 3 | 3 |
+--------+---------+
1 row in set (0.00 sec)
- b 圆周率
mysql> select pi();
+----------+
| pi() |
+----------+
| 3.141593 |
+----------+
1 row in set (0.00 sec)
mysql> select PI();
+----------+
| PI() |
+----------+
| 3.141593 |
+----------+
1 row in set (0.00 sec)
- c 开方函数
SQRT(X):返回X的平方根,负数返回null;
mysql> select sqrt(9),sqrt(34),SqRt(100),sqrt(-23);
+---------+-------------------+-----------+-----------+
| sqrt(9) | sqrt(34) | SqRt(100) | sqrt(-23) |
+---------+-------------------+-----------+-----------+
| 3 | 5.830951894845301 | 10 | NULL |
+---------+-------------------+-----------+-----------+
1 row in set (0.00 sec)
- d 取余函数
MOD(X,Y):返回 X/Y后的余数。
mysql> select MOD(45,6);
+-----------+
| MOD(45,6) |
+-----------+
| 3 |
+-----------+
1 row in set (0.00 sec)
- e 向上取整
mysql> select ceil(3.5);
+-----------+
| ceil(3.5) |
+-----------+
| 4 |
+-----------+
1 row in set (0.00 sec)
- f 向下取整
mysql> select floor(3.5);
+------------+
| floor(3.5) |
+------------+
| 3 |
+------------+
1 row in set (0.00 sec)
- g 随机数
随机数 RAND(X):X不一样,产生的随机数不一样
mysql> select ceil(rand(10)),rand(10),rand(10),rand();
+----------------+--------------------+--------------------+--------------------+
| ceil(rand(10)) | rand(10) | rand(10) | rand() |
+----------------+--------------------+--------------------+--------------------+
| 1 | 0.6570515219653505 | 0.6570515219653505 | 0.2740826301966632 |
+----------------+--------------------+--------------------+--------------------+
1 row in set (0.00 sec)
mysql> select ceil(rand(10)),rand(-1),rand(0),rand();
+----------------+--------------------+---------------------+--------------------+
| ceil(rand(10)) | rand(-1) | rand(0) | rand() |
+----------------+--------------------+---------------------+--------------------+
| 1 | 0.9050373219931845 | 0.15522042769493574 | 0.5871009468912156 |
+----------------+--------------------+---------------------+--------------------+
1 row in set (0.00 sec)
- h 四舍五入取整
round(x):四舍五入返回最接近x的整数;
round(x,y):四舍五入返回最接近x的整数,并保留小数点以后y位,
round(2.125,6): 四舍五入保存6位小数, 2.125000 小数点位数不够,自动补零。
mysql> select round(2.345),round(2.50001),round(2.125,2),round(2.125,6);
+--------------+----------------+----------------+----------------+
| round(2.345) | round(2.50001) | round(2.125,2) | round(2.125,6) |
+--------------+----------------+----------------+----------------+
| 2 | 3 | 2.13 | 2.125000 |
+--------------+----------------+----------------+----------------+
round(1,10):值为1,即没有小数的不会自动补零。若要实现自动补零,必须有一位小数位
mysql> select round(1,10);
+-------------+
| round(1,10) |
+-------------+
| 1 | 直接返回整数
+-------------+
1 row in set (0.00 sec)
mysql> select round(1.0,10);
+---------------+
| round(1.0,10) |
+---------------+
| 1.0000000000 | 自动补零
+---------------+
1 row in set (0.00 sec)
mysql> select ID as "编号", Name as "姓名", sqrt(ROUND(Salary,5)) as "工资平方根" from user;
+--------+-----------------+--------------------+
| 编号 | 姓名 | 工资平方根 |
+--------+-----------------+--------------------+
| 7 | 燕虹 | 83.66681970769535 |
| 8 | 鬼王 | 89.44271909999159 |
| 9 | 毒神 | 83.66600265340756 |
| 10 | 陆小琪 | 31.622776601683793 |
| 11 | 苍松好道人 | 83.66600265340756 |
| 12 | 周一仙 | 70.71067811865476 |
| 13 | 小环 | 54.772255750516614 |
| 15 | 笨蛋 | 1 |
| 16 | 笨笨 | 1 |
| 17 | 小本 | 31.622776601683793 |
| 18 | 小欢 | 31.622776601683793 |
| 19 | 小欢 | 31.622776601683793 |
| 20 | 小欢 | 31.622776601683793 |
| 21 | 小欢 | 31.622776601683793 |
| 24 | 1 | 1 |
| 25 | xiaoming | 124.71968569556292 |
| 26 | 1 | 1 |
+--------+-----------------+--------------------+
17 rows in set (0.00 sec)
- i 截取数据
truncate(x,n):截取数x至小数点后n位。截取同时不进行四舍五入
mysql> select truncate(200.12345,3);
+-----------------------+
| truncate(200.12345,3) |
+-----------------------+
| 200.123 |
+-----------------------+
1 row in set (0.00 sec)
若n为0,则截取所有小数
mysql> select truncate(23456.12345,0);
+-------------------------+
| truncate(23456.12345,0) |
+-------------------------+
| 23456 |
+-------------------------+
1 row in set (0.00 sec)
若n为负数,则截取小数点向左的n位,截取后的位设为0
mysql> select truncate(23756.12345,-3);
+--------------------------+
| truncate(23756.12345,-3) |
+--------------------------+
| 23000 |
+--------------------------+
1 row in set (0.00 sec)
- j 正负数判断
正数返回1,负数返回-1,0返回0
mysql> select sign(1.234),sign(-12),sign(0),sign(null),sign("d");
+-------------+-----------+---------+------------+-----------+
| sign(1.234) | sign(-12) | sign(0) | sign(null) | sign("d") |
+-------------+-----------+---------+------------+-----------+
| 1 | -1 | 0 | NULL | 0 |
+-------------+-----------+---------+------------+-----------+
1 row in set, 1 warning (0.00 sec)
- k 幂函数计算
pow(x,y):返回数x的y次幂
mysql> select pow(2,-1),pow(2,0),pow(2,1),pow(2,2),pow(2,3);
+-----------+----------+----------+----------+----------+
| pow(2,-1) | pow(2,0) | pow(2,1) | pow(2,2) | pow(2,3) |
+-----------+----------+----------+----------+----------+
| 0.5 | 1 | 2 | 4 | 8 |
+-----------+----------+----------+----------+----------+
1 row in set (0.00 sec)
exp(x):返回e的x方数
注明:e自然常数,是数学科的一种法则。约为2.71828
mysql> select exp(-1) , exp(0) , exp(1),exp(2);
+---------------------+--------+-------------------+------------------+
| exp(-1) | exp(0) | exp(1) | exp(2) |
+---------------------+--------+-------------------+------------------+
| 0.36787944117144233 | 1 | 2.718281828459045 | 7.38905609893065 |
+---------------------+--------+-------------------+------------------+
1 row in set (0.00 sec)
- l 对数运算
select logX(n):返回以X为底,n的对数;
mysql> select log10(0),log10(10),log10(100),log10(1000);
+----------+-----------+------------+-------------+
| log10(0) | log10(10) | log10(100) | log10(1000) |
+----------+-----------+------------+-------------+
| NULL | 1 | 2 | 3 |
+----------+-----------+------------+-------------+
1 row in set, 1 warning (0.00 sec)
log(n):返回自然数e的对数;
mysql> select log(1),log(exp(1)), log(exp(2)),log(exp(3));
+--------+-------------+-------------+-------------+
| log(1) | log(exp(1)) | log(exp(2)) | log(exp(3)) |
+--------+-------------+-------------+-------------+
| 0 | 1 | 2 | 3 |
+--------+-------------+-------------+-------------+
1 row in set (0.00 sec)
- m 弧度公式
mysql> select radians(90),radians(180),radians(360);
+--------------------+-------------------+-------------------+
| radians(90) | radians(180) | radians(360) |
+--------------------+-------------------+-------------------+
| 1.5707963267948966 | 3.141592653589793 | 6.283185307179586 |
+--------------------+-------------------+-------------------+
1 row in set (0.00 sec)
- n 弧度转角度
mysql> select degrees(pi()), degrees(pi()*2), degrees(pi()/2);
+---------------+-----------------+-----------------+
| degrees(pi()) | degrees(pi()*2) | degrees(pi()/2) |
+---------------+-----------------+-----------------+
| 180 | 360 | 90 |
+---------------+-----------------+-----------------+
1 row in set (0.00 sec)
- o 正弦函数,余弦函数
sin(x),cos(x);
2流程控制函数
if函数
if(str,str1,str2); 若str为true,返回str1,否则返回str2;
mysql> select if(23>21,"加班","放假");
+-----------------------------+
| if(23>21,"加班","放假") |
+-----------------------------+
| 加班 |
+-----------------------------+
1 row in set (0.00 sec)
ifnull(str1,str2); 函数
若果:str1为null,返回str2;否则返回str1;
mysql> select ifnull(1,2);
+-------------+
| ifnull(1,2) |
+-------------+
| 1 |
+-------------+
1 row in set (0.00 sec)
mysql> select ifnull(null,23);
+-----------------+
| ifnull(null,23) |
+-----------------+
| 23 |
+-----------------+
1 row in set (0.00 sec)
case when 选择函数
case str when str1 then result1 when str2 then result2 else result3 end :
如果str的值为st1 ,返回result1, 如果str的值为st2 ,返回result1,否则返回result3。
mysql> select case 1 when 7 then "星期日"when 6 then "星期六" when 5 then "星期五" when 1 then "星期一" else"星期二" end;
+---------------------------------------------------------------------------------------------------------------------------+
| case 1 when 7 then "星期日"when 6 then "星期六" when 5 then "星期五" when 1 then "星期一" else"星期二" end |
+---------------------------------------------------------------------------------------------------------------------------+
| 星期一 |
+---------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.06 sec)
mysql> select case 23 when 7 then "星期日"when 6 then "星期六" when 5 then "星期五" when 1 then "星期一" else"星期二" end;
+----------------------------------------------------------------------------------------------------------------------------+
| case 23 when 7 then "星期日"when 6 then "星期六" when 5 then "星期五" when 1 then "星期一" else"星期二" end |
+----------------------------------------------------------------------------------------------------------------------------+
| 星期二 |
+----------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
3系统函数
- 查看数据库版本
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.16 |
+-----------+
1 row in set (0.07 sec)
- 查看数据库当前连接ID
显示当前连接是第几个连接数据库的
mysql> select connection_id();
+-----------------+
| connection_id() |
+-----------------+
| 2 |
+-----------------+
1 row in set (0.06 sec)
- processlist命令 显示数据库当前线程线程的运行状态,连接状态。当前执行的语句及所用时间。若果是root用户,则能查看所有用户的连接信息
mysql> show processlist;
+----+------+----------------+------+---------+------+----------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+----------------+------+---------+------+----------+------------------+
| 2 | root | localhost:4126 | zoo | Query | 0 | starting | show processlist |
| 3 | root | localhost:5002 | NULL | Sleep | 7830 | | NULL |
| 4 | root | localhost:5004 | zoo | Sleep | 7676 | | NULL |
| 5 | root | localhost:5005 | zoo | Sleep | 7717 | | NULL |
| 6 | root | localhost:5091 | zoo | Sleep | 7378 | | NULL |
| 7 | root | localhost:5092 | zoo | Sleep | 2 | | NULL |
+----+------+----------------+------+---------+------+----------+------------------+
6 rows in set (0.00 sec)