数据处理函数

发布时间:2020-10-21 整理:编程之家
编程之家收集整理的这篇文章主要介绍了数据处理函数编程之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随编程之家小编过来看看吧!

常用的数据处理函数(单行处理函数,特点:输入一行输出一行)如下

Lower	转换小写
upper	转换大写
substr	取子串(substr(被截取的字符串,起始下标,截取的长度))
length	取长度
trim	去空格
str_to_date	将字符串转换成日期
date_format	格式化日期
format	设置千分位
round	四舍五入
rand()	生成随机数
Ifnull 	可以将null转换成一个具体值

一lower

查询员工,将员工姓名全部转换成小写。

mysql> select lower(ename) from emp;
+--------------+
| lower(ename) |
+--------------+
| smith        |
| allen        |
| ward         |
| jones        |
| martin       |
| blake        |
| clark        |
| scott        |
| king         |
| turner       |
| adams        |
| james        |
| ford         |
| miller       |
+--------------+

二upper

查询job为MANAGER的员工

mysql> select job from emp where job=upper('manager');
+---------+
| job     |
+---------+
| MANAGER |
| MANAGER |
| MANAGER |
+---------+

三substr

查询姓名以M开头所有的员工

mysql> select * from emp where substr(ename,1,1) = upper('m');
+-------+--------+----------+------+------------+---------+---------+--------+
| EMPNO | ENAME  | JOB      | MGR  | HIREDATE   | SAL     | COMM    | DEPTNO |
+-------+--------+----------+------+------------+---------+---------+--------+
|  7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 |     30 |
|  7934 | MILLER | CLERK    | 7782 | 1982-01-23 | 1300.00 |    NULL |     10 |
+-------+--------+----------+------+------------+---------+---------+--------+
substr(string string,num start,num length)

string:要截取的字符串

start:截取的起始位置

length:截取的长度,支持负数

四length

取得员工姓名长度为5

mysql> select * from emp where length(ename) = 5;
+-------+-------+----------+------+------------+---------+--------+--------+
| EMPNO | ENAME | JOB      | MGR  | HIREDATE   | SAL     | COMM   | DEPTNO |
+-------+-------+----------+------+------------+---------+--------+--------+
|  7369 | SMITH | CLERK    | 7902 | 1980-12-17 |  800.00 |   NULL |     20 |
|  7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 |     30 |
|  7566 | JONES | MANAGER  | 7839 | 1981-04-02 | 2975.00 |   NULL |     20 |
|  7698 | BLAKE | MANAGER  | 7839 | 1981-05-01 | 2850.00 |   NULL |     30 |
|  7782 | CLARK | MANAGER  | 7839 | 1981-06-09 | 2450.00 |   NULL |     10 |
|  7788 | SCOTT | ANALYST  | 7566 | 1987-04-19 | 3000.00 |   NULL |     20 |
|  7876 | ADAMS | CLERK    | 7788 | 1981-05-23 | 1100.00 |   NULL |     20 |
|  7900 | JAMES | CLERK    | 7698 | 1981-12-03 |  950.00 |   NULL |     30 |
+-------+-------+----------+------+------------+---------+--------+--------+

五trim

trim会去首尾空格,不会去除中间的空格

mysql> select * from emp where job = trim(upper('MANAGER   '));
+-------+-------+---------+------+------------+---------+------+--------+
| EMPNO | ENAME | JOB     | MGR  | HIREDATE   | SAL     | COMM | DEPTNO |
+-------+-------+---------+------+------------+---------+------+--------+
|  7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL |     20 |
|  7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL |     30 |
|  7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | NULL |     10 |
+-------+-------+---------+------+------------+---------+------+--------+

六str_to_date 

查询1981-02-20入职的员工(第一种方法,与数据库的格式匹配上)

mysql> select * from emp where hiredate = '1981-02-20';
+-------+-------+----------+------+------------+---------+--------+--------+
| EMPNO | ENAME | JOB      | MGR  | HIREDATE   | SAL     | COMM   | DEPTNO |
+-------+-------+----------+------+------------+---------+--------+--------+
|  7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 |     30 |
+-------+-------+----------+------+------------+---------+--------+--------+

查询1981-02-20入职的员工(第二种方法,将字符串转换成date类型)

mysql> select * from emp where hiredate = str_to_date('1981-02-20','%Y-%m-%d');
+-------+-------+----------+------+------------+---------+--------+--------+
| EMPNO | ENAME | JOB      | MGR  | HIREDATE   | SAL     | COMM   | DEPTNO |
+-------+-------+----------+------+------------+---------+--------+--------+
|  7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 |     30 |
+-------+-------+----------+------+------------+---------+--------+--------+

str_to_date可以将字符串转换成日期,具体格式str_to_date (字符串,匹配格式)

七date_format

查询1981-02-20以后入职的员工,将入职日期格式化成yyyy-mm-dd hh:mm:ss

mysql> select empno,ename,date_format(hiredate,'%Y-%m-%d %H:%i%s') as hiredate from emp;
+-------+--------+--------------------+
| empno | ename  | hiredate           |
+-------+--------+--------------------+
|  7369 | SMITH  | 1980-12-17 00:0000 |
|  7499 | ALLEN  | 1981-02-20 00:0000 |
|  7521 | WARD   | 1981-02-22 00:0000 |
|  7566 | JONES  | 1981-04-02 00:0000 |
|  7654 | MARTIN | 1981-09-28 00:0000 |
|  7698 | BLAKE  | 1981-05-01 00:0000 |
|  7782 | CLARK  | 1981-06-09 00:0000 |
|  7788 | SCOTT  | 1987-04-19 00:0000 |
|  7839 | KING   | 1981-11-17 00:0000 |
|  7844 | TURNER | 1981-09-08 00:0000 |
|  7876 | ADAMS  | 1981-05-23 00:0000 |
|  7900 | JAMES  | 1981-12-03 00:0000 |
|  7902 | FORD   | 1981-12-03 00:0000 |
|  7934 | MILLER | 1982-01-23 00:0000 |
+-------+--------+--------------------+
日期格式的说明
%Y:代表4位的年份
%y:代表2位的年份
 
%m:代表月,格式为(01……12)  
%c:代表月,格式为(1……12)
 
%H:代表小时,格式为(00……23)  
%h: 代表小时,格式为(01……12)  
  
%i: 代表分钟,格式为(00……59) 


%r:代表 时间,格式为12 小时(hh:mm:ss [AP]M)  
%T:代表 时间,格式为24 小时(hh:mm:ss) 


%S:代表 秒,格式为(00……59)  
%s:代表 秒,格式为(00……59) 

八format

查询员工薪水加入千分位和保留两位小数

mysql> select empno,format(sal,2) from emp;
+-------+--------+----------------+
| empno | ename  | format(sal,2) |
+-------+--------+----------------+
|  7369 | SMITH  | 800.00         |
|  7499 | ALLEN  | 1,600.00       |
|  7521 | WARD   | 1,250.00       |
|  7566 | JONES  | 2,975.00       |
|  7654 | MARTIN | 1,250.00       |
|  7698 | BLAKE  | 2,850.00       |
|  7782 | CLARK  | 2,450.00       |
|  7788 | SCOTT  | 3,000.00       |
|  7839 | KING   | 5,000.00       |
|  7844 | TURNER | 1,500.00       |
|  7876 | ADAMS  | 1,100.00       |
|  7900 | JAMES  | 950.00         |
|  7902 | FORD   | 3,000.00       |
|  7934 | MILLER | 1,300.00       |
+-------+--------+----------------+

九round

四色五入

取整

mysql> select round(123.45);
+---------------+
| round(123.45) |
+---------------+
|           123 |
+---------------+
保留一位有效小数

mysql> select round(123.45,1);
+------------------+
| round(123.45,1) |
+------------------+
|            123.5 |
+------------------+

十rand()

mysql> select rand();
+---------------------+
| rand()              |
+---------------------+
| 0.13592706633352403 |
+---------------------+

十一case … when … then …..else …end

如果jobMANAGERG薪水上涨10%,如果jobSALESMAN工资上涨50%

mysql> select empno,job,sal,case job when 'manager' then sal*1.1 when 'salesman' then sal*1.5
 end as newsal from emp;
+-------+--------+-----------+---------+---------+
| empno | ename  | job       | sal     | newsal  |
+-------+--------+-----------+---------+---------+
|  7369 | SMITH  | CLERK     |  800.00 |    NULL |
|  7499 | ALLEN  | SALESMAN  | 1600.00 | 2400.00 |
|  7521 | WARD   | SALESMAN  | 1250.00 | 1875.00 |
|  7566 | JONES  | MANAGER   | 2975.00 | 3272.50 |
|  7654 | MARTIN | SALESMAN  | 1250.00 | 1875.00 |
|  7698 | BLAKE  | MANAGER   | 2850.00 | 3135.00 |
|  7782 | CLARK  | MANAGER   | 2450.00 | 2695.00 |
|  7788 | SCOTT  | ANALYST   | 3000.00 |    NULL |
|  7839 | KING   | PRESIDENT | 5000.00 |    NULL |
|  7844 | TURNER | SALESMAN  | 1500.00 | 2250.00 |
|  7876 | ADAMS  | CLERK     | 1100.00 |    NULL |
|  7900 | JAMES  | CLERK     |  950.00 |    NULL |
|  7902 | FORD   | ANALYST   | 3000.00 |    NULL |
|  7934 | MILLER | CLERK     | 1300.00 |    NULL |
+-------+--------+-----------+---------+---------+

其他的工资不动,需要添加else

mysql> select empno,case job when 'manager' then sal*1.1 when 'salesman' then sal*1.5
 else sal end as newsal from emp;
+-------+--------+-----------+---------+---------+
| empno | ename  | job       | sal     | newsal  |
+-------+--------+-----------+---------+---------+
|  7369 | SMITH  | CLERK     |  800.00 |  800.00 |
|  7499 | ALLEN  | SALESMAN  | 1600.00 | 2400.00 |
|  7521 | WARD   | SALESMAN  | 1250.00 | 1875.00 |
|  7566 | JONES  | MANAGER   | 2975.00 | 3272.50 |
|  7654 | MARTIN | SALESMAN  | 1250.00 | 1875.00 |
|  7698 | BLAKE  | MANAGER   | 2850.00 | 3135.00 |
|  7782 | CLARK  | MANAGER   | 2450.00 | 2695.00 |
|  7788 | SCOTT  | ANALYST   | 3000.00 | 3000.00 |
|  7839 | KING   | PRESIDENT | 5000.00 | 5000.00 |
|  7844 | TURNER | SALESMAN  | 1500.00 | 2250.00 |
|  7876 | ADAMS  | CLERK     | 1100.00 | 1100.00 |
|  7900 | JAMES  | CLERK     |  950.00 |  950.00 |
|  7902 | FORD   | ANALYST   | 3000.00 | 3000.00 |
|  7934 | MILLER | CLERK     | 1300.00 | 1300.00 |
+-------+--------+-----------+---------+---------+

十二ifnull

计算员工的年薪

mysql> select sal*12 + comm from emp;
+---------------+
| sal*12 + comm |
+---------------+
|          NULL |
|      19500.00 |
|      15500.00 |
|          NULL |
|      16400.00 |
|          NULL |
|          NULL |
|          NULL |
|          NULL |
|      18000.00 |
|          NULL |
|          NULL |
|          NULL |
|          NULL |
+---------------+

SQL语句当中若有NULL值参与数学运算,计算结果一定是NULL

为了防止计算结果出现NULL,建议先使用ifnull空值处理函数预先处理。

正确的slq语句如下:

mysql> select sal*12 + ifnull(comm,0) from emp;
+--------------------------+
| sal*12 + ifnull(comm,0) |
+--------------------------+
|                  9600.00 |
|                 19500.00 |
|                 15500.00 |
|                 35700.00 |
|                 16400.00 |
|                 34200.00 |
|                 29400.00 |
|                 36000.00 |
|                 60000.00 |
|                 18000.00 |
|                 13200.00 |
|                 11400.00 |
|                 36000.00 |
|                 15600.00 |
+--------------------------+

总结

以上是编程之家为你收集整理的数据处理函数全部内容,希望文章能够帮你解决数据处理函数所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您喜欢交流学习经验,点击链接加入编程之家官方QQ群:1065694478
编程之家官方公众号

微信公众号搜索 “ 程序精选 ” ,选择关注!

微信公众号搜索 “ 程序精选 ”
精选程序员所需精品干货内容!