概念:类似于java中的方法,将一组逻辑语句封装在方法体中,对外暴露方法名
好处:1、隐藏实现细节 2、提高代码的重用性
调用语法:
select 函数名(实参列表) 【from 表】;
分类:
1、单行函数
如:concat、length、ifnull等
- 字符函数
1、length:获取参数值的字节个数
2、concat:拼接字符串
3、upper/lower:将字符变为大写/小写
- 案例一:将姓变为大写,名变为小写,然后拼接
4、substr/substring:截取字符串select concat(upper(last_name),lower(first_name)) 姓名 from employees;
注意:索引从1开始
b. 案例二:姓名中首字符大写,其它字符小写然后用下划线拼接显示出来select substr('李莫愁爱上了陆展元',7) out_put;#输出为 陆展元#截取从指定索引处后面的所有字符select substr('李莫愁爱上了陆展元',1,3) out_put;#输出为 李莫愁#截取从指定索引处指定字符长度的字符
5、instr:返回子串第一次出现的索引,如果找不到,则返回0select concat(upper(substr(last_name,1,1)),'_',lower(substr(last_name,2))) out_putfrom employees;
6、trim:去掉前后字符,默认去空格select instr('杨不悔爱上了殷六侠','殷六侠') AS out_put;#输出结果为7
7、lpad/rpad:用指定的字符实现左/右填充指定长度select trim(' 张翠山 ') as out_put;#输出结果为张翠山select trim('a' from 'aaaaaaaa张aaaaaaa翠aaaaaaa山aaaaa') as out_put;
8、replace:替换select lpad('殷素素',10,'*') as out_put;
select replace('周芷若周芷若张无忌爱上了周芷若','周芷若','赵敏') as out_put;#输出为赵敏赵敏张无忌爱上了赵敏
- 数学函数
1、round:四舍五入
2、ceil:向上取整,返回>=该参数的最小整数
3、floor:向下取整,返回<=该参数的最大整数
4、truncate:截断 select truncate(1.69999,1);返回值为1.6
5、mod:取余
- 日期函数
1、now:返回当前系统日期+时间
2、curdate:返回当前系统日期,不包含时间
3、curtime:返回当前时间,不包含日期
4、year/month/day/hour/minute
5、str_to_date:将日期格式的字符转换为指定格式的日期
str_to_date('9-13-1999','%m-%d-%Y');#输出为 1999-09-13

案例一:查询入职日期为1992-4-3的员工信息
select * from employees where hiredate='1992-4-3';select * from employees where hiredate=str_to_date('4-3 1992','%c-%d %Y');
6、date_format:将日期转换为字符
date_format('2018/6/6','%Y年%m月%d日');#输出为2018年06月06日
案例二:查询有奖金的员工名和入职日期(xx月/xx日 xx年)
select last_name,date_format(hiredate,'%m月/%d日 %y年')from employeeswhere commission_pct is not null;
- 其他函数
select version();
select database();
select users;
- 流程控制函数
1、if:if else 的效果
select if(10>5,'大','小');#返回值为大select last_name,commission_pct,if(commission_pct is null,'没奖金','有奖金')from employees;
2、case:
switch case 的效果
case 要判断的字段或表达式when 常量1 then 要显示的值1或者语句1when 常量2 then 要显示的值2或者语句2...else 要显示的值n或语句n;end
案例一:查询员工的工资,要求:部门号=30,显示的工资为1.1倍,部门号=40,显示的工资为1.2倍,部门号=50,显示的工资为1.3倍,其它的部门显示的工资为原工资
select salary 原始工资,department_id,case department_idwhen 30 then salary*1.1when 40 then salary*1.2when 50 then salary*1.3else salaryend as 新工资from employees;
b. 类似于多重if ```sql case函数的使用二:类似于多重if
case when 条件1 then 要显示的值1或语句1 when 条件2 then 要显示的值2或语句2 … else 条件n then 要显示的值n或语句n end
1. 案例二:查询员工的工资,如果工资>20000,显示A级别,如果工资>15000,显示B级别,如果工资>10000,显示C级别,否则显示D级别```sqlselect salary,casewhen salary>20000 then 'A'when salary>15000 then 'B'when salary>10000 then 'C'else 'D'end as 工资级别from employees;
例题:
1、显示系统时间(注:日期+时间)
select now();
2、查询员工号、姓名、工资以及工资提高百分之20%以后的结果(new salary)
select employee_id,last_name,salary,salary*1.2 "new salary"from employees;
3、将员工的姓名按首字母排序,并写出姓名的长度(length)
select length(last_name),substr(last_name,1,1) 首字符,last_namefrom employeesorder by 首字符;
4、做一个查询,产生下面结果:
select concat(last_name,' earns ',salary,' monthly but wants ',salary*3)as "Dream Salary"from employees;
5、使用case-when,按照下面的条件:
job grade
AD_PRES A
ST_MAN B
IT_PROG C
select job_id as job,case job_idwhen 'AD_PRES' then 'A'when 'ST_MAN' then 'B'when 'IT_PROG' then 'C'end as gradefrom employees;
2、 分组函数
做统计使用,又称为统计函数/聚合函数/组函数
分类:sum 求和、avg 平均值、max 最大值、min 最小值、count 计算个数
- 1、简单的使用: ```sql select sum(salary) from employees; select avg(salary) from employees; select min/max(salary) from employees; select count(salary) from employees;
select sum(salary) 和,select avg(salary) 平均值 from employees;
- 2、参数支持哪些类型sum、avg一般用于处理数值型<br />max、min、count可以处理任何类型<br />以上分组函数都忽略null值- 3、和distinct搭配```sqlselect sum(distinct salary),sum(salary) from employees;
4、count函数单独介绍
select count(salary) from employees;select count(*) from employees; #统计行数select count(1) from employees; #相当于在表中加了一列这样子的常数值,再进行count
5、和分组函数一同查询的字段有限制,要求是group by后的字段
select avg(salary),employee_id from employees;#这是不对的,因为结果输出应该为一个表格,但是avg的输出就一个值
例题:
1、查询公司员工工资的最大值、最小值、平均值和总和select max(salary) mx_sal,min(salary) mi_sal,avg(salary) ag_sal,sum(salary) sum_salfrom employees;
2、查询员工表中的最大入职时间和最小入职时间的相差天数(DIFFERENCE)
select datadiff(max(hiredate),min(hiredate)) DIFFERENCEfrom employees;
3、查询部门编号为90的员工个数
select count(*)from employeeswhere department_id=90;
