mysql进阶4:常见函数

2020-04-19  本文已影响0人  弦好想断

/*
概念:类似于Java中的方法,将一组逻辑语句封装在方法体中,对外暴露方法名
好处:1隐藏了实现细节 2.提高代码的重用性
调用:select 函数名(实参列表)【from 表】
特点:函数名 函数的功能
分类:单行函数:concat、length、ifnull
分组函数(统计函数):做统计使用
*/

use myemployees;
show databases;

一、字符函数

1.length 获取参数值的字节个数(utf8中一个汉字占3个字节)

select length('join');
select length('张三丰hahhah');
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 'aaaa张a翠a山aaaaa') as out_put;

7.lpad 用指定字符实现左填充指定长度

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

在右边截断

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

8.rpad

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

9.replace:替换

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

二.数学函数

round 四舍五入

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

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

select ceil(1.42);
select ceil(-1.02);

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

select floor(1.42);
select floor(-1.02);

truncate 截断

select truncate(1.69999,1);

mod 取余 a-a/b*b

select mod(10,3);
select mod(-10,3);
select mod(10,-3);

三.日期函数

now 返回当前系统日期+时间

select now();

curdata 返回当前日期,不包含时间

select curdate();

curtime 返回当前时间,不包含日期

select curtime();

可以获取指定的部分,年、月、日、小时、分钟、秒

select year(now()) 年;
select year('1998-01-01') 年;
select year(hiredate) 年 from employees;
select month(hiredate) 月 from employees;
select monthname(now()) 月;
select dayname(now()) 日;

str_to_date 将字符通过指定的格式转换成日期

select str_to_date('1992-3-2','%Y-%c-%d') as out_put;

查询入职日期为1992-3-2的员工信息

select * from employees where hiredate = '1992-4-3';
select * from employees where hiredate = str_to_date('4-3 1992','%c-%d %Y');

data_format 将日期转换成字符

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

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

select last_name,date_format(hiredate,'%m月/%d日 %y年') as 入职日期 from employees;

其他函数

select version();
select database();
select user();

流程控制函数

1.if函数

select if(10>5,'大','小');
select last_name,commission_pct,if(commission_pct is null,'没奖金,呵呵','有奖金,嘻嘻') 
as 备注 from employees;

2.case函数 使用一:switch case的效果

/*
case 要判断的字段和表达式
when 常量1,then 要显示的值1或语句1;
when 常量2,then 要显示的值2或语句2;
。。。
else 要显示的值n或语句n;
end
*/

案例:查询员工的工资,要求

部门号为30,显示地工资为1.1倍

部门号为49,显示的工资为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;

case函数的使用2:类似于if

案例:查询员工的工资级别,如果工资大于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;
上一篇下一篇

猜你喜欢

热点阅读