概念:

类似于java的方法,将一组逻辑语句封装在方法体中,对外暴露方法名
好处:1、隐藏了实现细节 2、提高代码的重用性
调用:select 函数名(实参列表) 【from 表】;

特点:

  1. ①叫什么(函数名)<br /> ②干什么(函数功能)

分类:

1、单行函数

如 concat、length、ifnull等

2、分组函数

功能:做统计使用,又称为统计函数、聚合函数、组函数

常见函数:

一、单行函数

字符函数:

length:获取字节个数(utf-8一个汉字代表3个字节,gbk为2个字节)<br />    concat<br />    substr<br />    instr<br />    trim<br />    upper<br />    lower<br />    lpad<br />    rpad<br />    replace

数学函数:

round<br />    ceil<br />    floor<br />    truncate<br />    mod

日期函数:

now<br />    curdate<br />    curtime<br />    year<br />    month<br />    monthname<br />    day<br />    hour<br />    minute<br />    second<br />    str_to_date<br />    date_format

其他函数:

version<br />    database<br />    user

控制函数

if<br />    case

一、字符函数

1.length 获取参数值的字节个数

select length('johh');
select length('张三丰hahhaha');
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(last_name,2))) out_put from employees

5.instr 返回子串第一次出现的索引,如果找不到返回0

select instr('杨不殷六侠悔爱上了殷六侠','殷八侠') as out_put;

6.trim

select length(trim('  张翠山   ')) as out_put;
SELECT TRIM('a' FROM 'aaaaaaaaa张aaaaaaaaaaaa翠山aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')  AS out_put;

7.lpad 用指定的字符实现左填充指定长度(不够才填充)

select lpad('殷素素',2,'*') as out_put;

8.rpad 用指定的字符实现右填充指定长度

select rpad('殷素素',12,'ab') as out_put;

9.replace 替换

select replace('周芷若周芷若周芷若周芷若张无忌爱上了周芷若','周芷若','赵敏') as out_put;

二、数学函数

round 四舍五入

select round(-1.55);
select round(1.567,2);

ceil 向上取整,返回>=该参数的最小整数

select ceil(-1.02);

floor 向下取整,返回<=该参数的最大整数

select floor(-9.99);

truncate 截断

select truncate(1.69999,1);

mod取余

mod(a,b) : a-a/bb
mod(-10,-3):-10- (-10)/(-3)
(-3)=-1

select mod(10,-3);
select 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-3','%Y-%c-%d') as out_put;

查询入职日期为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');

date_format 将日期转换成字符

select date_format(now(),'%y年%m月%d日') as out_put;

查询有奖金的员工名和入职日期(xx月/xx日 xx年)

select last_name,date_format(hiredate,'%m月/%d日 %y年') 入职日期 from employees where commission_pct is not null;

四、其他函数

select version(); --数据库版本
select database();--目前使用的数据库
select user();--用户名目前

五、流程控制函数

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 要显示的值2或语句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){
语句1;
}else if(条件2){
语句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,
case
when salary>20000 then 'A'
when salary>15000 then 'B'
when salary>10000 then 'C'
else 'D'
end as 工资级别
from employees;