**
函数的调用:select 函数名(实参列表)from 表; 函数的分类: 1.单行函数如:concat, length, iffull等 2,分组函数,功能是做统计使用的,又称为统计函数,聚合函数,组函数
**
select trim(' 沈同学 ') as out_put; #去空格,trim()函数
select substr('冰敦敦和雪融融',1,3) out_put #3指3个数,结果是冰敦敦
select trim ('a' from 'aaaaa沈aaa同学aaaaaaaa') as out_put #去掉前后的aaaa
select ipad('沈同学',10,'*') as out_put;#ipad用指定的字符实现左填充指定长度为10
select replace('张无忌爱上了周芷若','周芷若','赵敏') as out_put #替换
案例
SELECT CONCAT(UPPER(SUBSTR(客戶,1,1)),'_' , LOWER(SUBSTR(客戶,2))) as out_put
FROM 商机; #客户的首字母用大写,其他字母用小写,然后再用_拼接
二,数学函数
#四舍五入
select round(1.45);
select round(-1.55); #-2
select round(1.557,2); #1.56
#ceil 向上取整返回大于等于该参数的最小整数
select ceil(1.52);
#floor 向下取整 返回<=该参数的最大整数
select floor (-9.99);
#truncate截断
select truncate(1.65,1);
#取余
select mod(10,3) ; #1
三,日期函数
#now 返回当前系统的日期+时间
select now();
#curdate 返回当前系统的日期不包含时间
select curdate();
#curtime 返回当前系统的时间,不包含日期
select curtime();
#可以获取时间指定的部分,年,月,日,小时,分钟,秒
select year(now()) 年;
#str_to_date 将字符通过指定格式转化为日期
select str_to_date('1998-3-2','%Y-%c-%d') as out_put;
#查询员工入职日期为1992-3-2的信息
select *from employees where hiredate= str_to_date('4-3 1992','%c-%d-%Y');
#date_format 将日期转化为字符
select date_format(now(),'%y年%m月%d日') as out_put;
#查询有奖金的员工名字和入职日期(xx月/xx日 xx年)
select name,date_format(hiredata,'%m月/%d日 %y年) 入职日期
from employees
where commission_pct is not null;
其他函数
流程控制函数
if 函数:if else的效果
select if (10<5,'大','小');
select name ,commission, if(commmission is null,'没奖金','有奖金') 备注
from employees;
**
case 函数的使用一
**
案例:查询员工的工资,要求
部门号=30,显示的工资为1.1倍
部门号=40 ,显示的工资为1.2倍
部门号=50,显示的工资为1.3倍
其他部门,显示的工资为原工资
select salary 原始工资,department,
case department
when 30 then salary*1.1
when 40 then salary*1.2
when 50 then salary*1.3
else salary
end as 新工资
from employees;
case 函数的使用二(用于区间判断)
案例:如果工资>20000,显示A级别‘,
如果工资>15000,显示b级别
如果工资>10000,显示c级别
否则显示d级别
’
select salary,
case
when salary>20000 then 'a'
when salary >15000 then 'b'
when salary >10000 then 'c'
else 'd'
end as 工资级别
from employees;