# 进阶4:常见函数
/*
概念:类似与java的方法,将一组逻辑语句封装在方法体中,对外暴露方法名
好处:1、隐藏了实现细节 2、提高了代码的重用性
调用:select 函数名(实参列表) 【from 表】;
特点:
1、叫什么(函数名)
2、干什么(分组函数)
分类:
1、单行函数
如 concat、length、ifnull等
2、分组函数
功能:做统计使用,又称为统计函数、聚合函数、组函数
常见函数:
单行函数:
字符函数:
length
concat
substr
instr
trim
upper
lower
lpad
rpad
replace
数据函数:
round
ceil
floor
truncate
mod
日期函数:
now
curdate
cyrtime
year
month
monthname
day
hour
minute
second
str_to_date
date_format
其他函数:
version
database
user
控制函数:
if
case
分组函数:
*/
# 一、字符函数
# 1. length
select length('john');
select length('张三丰hahaha');
show variables like '%char%';
# 2. concat 拼接字符串
select concat(last_name,'_',first_name) from employees;
# 3. upper、lower
select upper('john');
select lower('JOHN');
# 示例:将姓变大写,名变小写,然后拼接
select concat(upper(last_name),'_',lower(first_name)) 姓名 from employees;
# 4. substr、substring
# 注意:索引从1开始
# 截取从指定索引处后面所有字符
select substr('李莫愁爱上了陆展元',7) out_put;
# 截取从指定字符长度的字符
select substr('李莫愁爱上了陆展元',1,3) out_put;
# 案例:姓名中首字符大写,其他字符小写然后用——拼接,显示出来
select concat(upper(substr(last_name,1,1)),'_',lower(substr(first_name,2))) out_put
from employees;
# 5. instr 返回字串第一次出现的索引,如果找不到返回0
select instr('杨不悔爱上了殷六侠','殷六侠') as out_put
from employees;
# 6. trim
select length(trim(' 张翠山 ')) as out_put;
select trim('a' from 'aaaaaaaaaaa张aaaaaaaa翠山aaaaaaaaaaaaaaaa') as out_put;
# 7. lpad 用指定的字符实现左填充指定的长度
select lpad('殷素素',10,'*') as out_put;
select lpad('殷素素',2,'*') as out_put;
# 8. rpad 用指定的字符实现右填充指定的长度
select rpad('殷素素',10,'*') as out_put;
select rpad('殷素素',2,'*') as out_put;
# 9. replace 替换
select replace('周芷若周芷若周芷若周芷若张无忌爱上了周芷若','周芷若','赵敏') as out_put;
# 二、数学函数
# round 四舍五入
select round(1.65);
select round(1.45);
select round(-1.45);
select round(-1.55);
select round(-1.576,2);
# ceil 向上取整,返回>=该参数的最小取整数
select ceil(1.00);
select ceil(1.02);
select ceil(-1.02);
# floor 向下取整,返回<=该参数的最大整数
select floor(9.99);
select floor(-9.99);
# truncate 截断
select truncate(1.66,1);
select truncate(1.6999999,1);
# mod 取余
select 10%3;
select mod(10,3);
select mod(-10,-3);
select mod(10,-3);
# 三、日期函数
# now 返回当前系统日期 + 时间
select now();
# curdate 返回当前系统日期,不包含时间
select curdate();
# curtime 返回当前时间,不包含日期
select curtime();
# 可以获取指定的部分,年、月、日、小时、分钟、秒
select year(now()) 年;
select year('1998-1-1') 年;
select year(hiredate) 年 from employees;
select month(now()) 月;
select monthname(now()) 月;
# str_to_date 将字符通过指定的格式转换成日期
select str_to_date('1998-3-2','%Y-%c-%d') out_put;
# 查询入职日期为1992-4-3的员工信息
select * from employees where hiredate = '1992-4-3';
select * from employees where hiredate = str_to_date('1992-4-3','%Y-%c-%d');
select * from employees where hiredate = str_to_date('4-3 1992','%c-%d %Y');
# date_format 将日期转换成字符
select date_format(now(),'%y年%m月%d日') out_put;
# 查询有奖金的员工名和日期(xx月/xx日 xx年)
select last_name,date_format(hiredate,'%m月/%d日 %y年') from employees where commission_pct is null;
# 四、其他函数
select version();
select database();
# 五、流程控制函数
# 1. if函数:if else 的效果
select if(10>5,'大','小');
select last_name,commission_pct,if(commission_pct is null,'没奖金 哈哈','有奖金 嘻嘻') 备注
from employees;
# 2. case函数的使用一:switch case 的效果
/*
java中
switch(变量或表达式){
case 常量1:语句1:break;
...
default:语句n;break;
}
mysql中
case 要判断的字段或表达式
when 常量1 then 要显示的值1或语句1;
when 常量2 then 要显示的值1或语句2;
...
else 要显示的值n或语句n;
end
*/
/* 案例:查询员工的工资,要求
部门号=30,显示的工资为1.1倍
部门号=40,显示的工资为1.2倍
部门号=50,显示的工资为1.3倍
其他部门,显示的工资为原工资
*/
select salary 原始工资,department_id,
case department_id
when 30 then salary*1.1
when 40 then salary*1.2
when 50 then salary*1.3
else salary
end as 新工资
from employees;
# 3. case 函数的使用二:类似与 多重if
/*
java中
if(条件){
语句1;
}else if{
语句2;
}
...
else {
语句n;
}
mysql中:
case
when 条件1 then 要显示的值1或语句1
when 条件2 then 要显示的值2或语句2
...
else 要显示的值n或语句n;
end
*/
/*
案例:查询员工的工资的情况
如果工资>20000,显示A级别
如果工资>15000,显示B级别
如果工资>10000,显示C级别
否则,显示D级别
*/
select salary 原始工资,department_id,
case
when 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%后的结果(nwe salary)
select employee_id,last_name,salary,salary*0.2 as 'new salary' from employees;
# 3. 将员工的姓名按首字母排序,并写出姓名的长度(length)
select last_name,length(last_name) as 'length',substr(last_name,1,1) 首字符 from employees order by 首字符 desc;
/*
4. 做一个查询,产生下面的结果
<last_name> earns <salary> monthly but wants <sallary*3>
Dream Salary
King earns 24000 monthly but wants 72000
*/
select concat(lower(replace(last_name,'_','')),' earns ',salary,' monthly but wants ',salary*3) as "Dream Salary" from employees where salary='24000';
/*
5.使用case-when,按照下面的条件:
job grade
AD_PRES A
ST_MAN B
IT_PROG C
SA_REP D
ST_CLERK E
产生下面的结果
last_name job_id Grade
KILL AD_PRES A
*/
select last_name ,job_id,case job_id
when 'AD_PRES' then 'A'
when 'ST_MAN' then 'B'
when 'IT_PROG' then 'c'
when 'SA_REP' then 'D'
when 'ST_CLERK' then 'E'
end as 级别
from employees where job_id='AD_PRES';
# 二、分组函数
/*
功能:用作统计,又称为聚合函数或统计函数或组函数
分类:
sum 求和、avg 平均值、max 最大值、min 最小值、count 计算个数
特点:
1、sum、avg一般用于处理数值型
max、min、count可以处理任何类型
2、以上分组函数都忽略null值
3、可以和distinct搭配实现去重的运算
4、count函数的单独介绍
一半使用count(*)用作统计行数
5、和分组函数一同查询的字段要求是group by后的字段
*/
# 1、简单的使用
select sum(salary) from employees;
select avg(salary) from employees;
select max(salary) from employees;
select min(salary) from employees;
select count(salary) from employees;
select sum(salary) 和, avg(salary) 平均, max(salary) 最高, min(salary) 最低,count(salary) 个数
from employees;
# 2、参数支持哪些类型
select sum(last_name), avg(last_name) from employees;
select sum(hiredate), avg(hiredate) from employees;
select max(last_name), min(last_name) from employees;
select max(hiredate)l, min(hiredate) from employees;
select count(commission_pct) from employees;
select count(last_name) from employees;
# 3、忽略null
select sum(commission_pct), avg(commission_pct),sum(commission_pct)/35,sum(commission_pct)/107 from employees;
select max(commission_pct), min(commission_pct) from employees;
# 4、和distinct搭配
select sum(distinct salary),sum(salary) from employees;
select count(distinct salary), count(salary) from employees;
# 5、count函数的详细介绍
select count(salary) from employees;
select count(*) from employees;
select count(1) from employees;
/*
效率:
MYISAM 存储引擎下,count(*) 的效率高
INNODB 存储引擎下,count(*) 和 count(1) 的效率差不多,比count(字段)要高一些
*/
# 6、和分组函数一同查询的字段有限制
# 错误:select avg(salary),employee_id from employees;
# 测试
# 1. 查询公司员工工资的最大值、最小值、平均值、总和
select max(salary) 最大值, min(salary) 最小值, round(avg(salary),2) 平均值, sum(salary) 总和
from employees;
# 2. 查询员工表中的最大入职时间和最小入职时间的相差天数(diffrence)
select datediff(max(hiredate), min(hiredate)) diffrence from employees;
# 3. 查询部门编号为90的员工个数
select count(*) from employees where department_id=90;