单行处理函数/数据处理函数 常见的单行处理函数:
Lower
转换小写
upper
转换大写
substr
取子串(substr(被截取的字符串,起始下标,截取的长度))
length
取长度
trim
去空格
str_to_data
将字符串转换为日期
date_format
格式化日期
format
设置千分位
round
四舍五入
rand()
生成随机数
Ifnull
可以将null转换为成一个具体值
concat
函数进行字符串的拼接
case..when..then…when…then..else..end
当什么情况,怎么做,当什么情况,怎么做,其他情况怎么做,结束。
单行处理函数的特点:一个输入对应一个输出。
和单行处理函数相对的是:多行处理函数。(多行处理函数特点:多个输入对应一个输出。)
lower 转换小写 upper 转换大写 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 MariaDB [hello]> select lower(ename) from emp; +--------------+ | lower(ename) | +--------------+ | smith | | allen | | ward | | jones | | martin | | blake | | clark | | scott | | king | | turner | | adams | | james | | ford | | miller | +--------------+ 14 rows in set (0.000 sec) //14个输入,对应14个输出。
substr 取子串 用法:substr(被截取的字符串,起始下标,截取的长度)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 MariaDB [hello]> select substr(ename,1,1) from emp; +-------------------+ | substr(ename,1,1) | +-------------------+ | S | | A | | W | | J | | M | | B | | C | | S | | K | | T | | A | | J | | F | | M | +-------------------+ 14 rows in set (0.000 sec) //括号内三个参数一一对应,截取的字符串为ename字符串,起始下标从1开始,截取长度为1. MariaDB [hello]> select ename from emp where substr(ename,1,1) ='A'; +-------+ | ename | +-------+ | ALLEN | | ADAMS | +-------+ 2 rows in set (0.000 sec) //同样,也可以用来查找员工信息,像这一题,可以用来查找首字母是A的员工信息。(like模糊查询也行,法一法二)
注意:起始下标从1开始,没有0.
concat 函数进行字符串拼接 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 MariaDB [hello]> select concat (empno,ename) from emp; +----------------------+ | concat (empno,ename) | +----------------------+ | 7369SMITH | | 7499ALLEN | | 7521WARD | | 7566JONES | | 7654MARTIN | | 7698BLAKE | | 7782CLARK | | 7788SCOTT | | 7839KING | | 7844TURNER | | 7876ADAMS | | 7900JAMES | | 7902FORD | | 7934MILLER | +----------------------+ 14 rows in set (0.000 sec) MariaDB [hello]> select concat (lower(substr(ename,1,1)),substr(ename,2,length(ename)-1)) as result from emp; +--------+ | result | +--------+ | sMITH | | aLLEN | | wARD | | jONES | | mARTIN | | bLAKE | | cLARK | | sCOTT | | kING | | tURNER | | aDAMS | | jAMES | | fORD | | mILLER | +--------+ 14 rows in set (0.001 sec) //首字母小写法,当然你要改成首字母大写也可以。运用到字符串拼接原理,将第一个字母进行小写,之后与后面的字符串进行拼接,中间还运用了length函数对名字字符串进行测量,方面取第一位字母后面的长度。
上面还运用的多个函数嵌套,多多观察
length 取长度 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 MariaDB [hello]> select length(ename) as "长度" from emp; +------+ | 长度 | +------+ | 5 | | 5 | | 4 | | 5 | | 6 | | 5 | | 5 | | 5 | | 4 | | 6 | | 5 | | 5 | | 4 | | 6 | +------+ 14 rows in set (0.001 sec)
trim 去空格 1 2 3 4 5 6 7 8 9 10 11 MariaDB [hello]> select * from emp where ename=' king'; Empty set (0.001 sec) //因为多了一个空格导致没有查找到。 MariaDB [hello]> select * from emp where ename=trim(' king'); +-------+-------+-----------+------+------------+---------+------+--------+ | EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO | +-------+-------+-----------+------+------------+---------+------+--------+ | 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL | 10 | +-------+-------+-----------+------+------------+---------+------+--------+ 1 row in set (0.001 sec) //合理的进行了去空格操作。
str_to_data
将字符串转换为日期
date_format
格式化日期
format
设置千分位
上面三个则会放到后面再讲。
round 四舍五入 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 MariaDB [hello]> select "haihai" from emp; +--------+ | haihai | +--------+ | haihai | | haihai | | haihai | | haihai | | haihai | | haihai | | haihai | | haihai | | haihai | | haihai | | haihai | | haihai | | haihai | | haihai | +--------+ 14 rows in set (0.001 sec) //select后面跟着字面量的话,会自动生成与emp表列数一致,内容与字面量一致的表格。 MariaDB [hello]> select 100 from emp; +-----+ | 100 | +-----+ | 100 | | 100 | | 100 | | 100 | | 100 | | 100 | | 100 | | 100 | | 100 | | 100 | | 100 | | 100 | | 100 | | 100 | +-----+ 14 rows in set (0.001 sec) //数字也会被当做字面量。 //结论:select后面可以跟某个表的字段名,(可以等同看作变量名),也可以跟字面量/字面值(数据)。 MariaDB [hello]> select round(11.23,0) from emp; +----------------+ | round(11.23,0) | +----------------+ | 11 | | 11 | | 11 | | 11 | | 11 | | 11 | | 11 | | 11 | | 11 | | 11 | | 11 | | 11 | | 11 | | 11 | +----------------+ 14 rows in set (0.001 sec) //round函数(),第一个数字输入数字,第二个输入位数,0则保留到各位,1则保留到小数点后一位, -1则反向保留到十位。
rand() 生成随机数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 MariaDB [hello]> select rand() from emp; +---------------------+ | rand() | +---------------------+ | 0.03390393409310275 | | 0.5426893956425501 | | 0.6117352066670873 | | 0.43060787714143084 | | 0.3178337964395376 | | 0.2973453815070404 | | 0.533225548950234 | | 0.7740916309636939 | | 0.27078153870141275 | | 0.03163283134962696 | | 0.3458195713775415 | | 0.6341993935724715 | | 0.13353828446337793 | | 0.7650932723331203 | +---------------------+ 14 rows in set (0.001 sec) MariaDB [hello]> select round(rand()*100,0) from emp; +---------------------+ | round(rand()*100,0) | +---------------------+ | 34 | | 20 | | 100 | | 38 | | 93 | | 47 | | 60 | | 57 | | 4 | | 50 | | 37 | | 36 | | 68 | | 33 | +---------------------+ 14 rows in set (0.011 sec) //100以内的随机数字。
ifnull 可以将null转换成一个具体的值 ifnull是空处理函数,专门处理空的。
在所有数据库中,只要有NULL参与的数学运算,最终结果就是NULL。
为了避免这一现象,就需要使用ifnull函数。
用法:ifnull(数据,被当作哪一个值)
如果“数据”为null时,把这个数据当作哪一个值。
eg:计算每个员工的年薪?
年薪=(月薪+月补助)*12;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 MariaDB [hello]> select ename,(sal+ifnull(comm,0))*12 as "年薪" from emp; +--------+----------+ | ename | 年薪 | +--------+----------+ | SMITH | 9600.00 | | ALLEN | 22800.00 | | WARD | 21000.00 | | JONES | 35700.00 | | MARTIN | 31800.00 | | BLAKE | 34200.00 | | CLARK | 29400.00 | | SCOTT | 36000.00 | | KING | 60000.00 | | TURNER | 18000.00 | | ADAMS | 13200.00 | | JAMES | 11400.00 | | FORD | 36000.00 | | MILLER | 15600.00 | +--------+----------+ 14 rows in set (0.001 sec)
case…when…then…when..then..else…end… 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 MariaDB [hello]> select ename,job,(case job when "MANAGER" then sal*1.1 else sal end) as new from emp; +--------+-----------+---------+ | ename | job | new | +--------+-----------+---------+ | SMITH | CLERK | 800.00 | | ALLEN | SALESMAN | 1600.00 | | WARD | SALESMAN | 1250.00 | | JONES | MANAGER | 3272.50 | | MARTIN | SALESMAN | 1250.00 | | BLAKE | MANAGER | 3135.00 | | CLARK | MANAGER | 2695.00 | | SCOTT | ANALYST | 3000.00 | | KING | PRESIDENT | 5000.00 | | TURNER | SALESMAN | 1500.00 | | ADAMS | CLERK | 1100.00 | | JAMES | CLERK | 950.00 | | FORD | ANALYST | 3000.00 | | MILLER | CLERK | 1300.00 | +--------+-----------+---------+ 14 rows in set (0.001 sec)