欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mysql常见函数及其用例

程序员文章站 2022-05-15 13:37:02
函数调用:select 函数名(实参列表) 【from 表】; 函数分类: 1、单行函数 如 concat、length、ifnull等。 2、分组函数 功能:做统计使用,又称为统计函数、聚合函数、组函数。 常见函数: 一、单行函数 字符函数: 数学函数: 日期函数: 其他函数: 控制函数: 一、字 ......

函数调用:select 函数名(实参列表) from 表】;

函数分类:

1、单行函数

concatlengthifnull等。

2、分组函数 

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

 

常见函数:

一、单行函数

字符函数:

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

数学函数:

round
ceil
floor
truncate
mod

日期函数:

now
curdate
curtime
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'); 

 

2.concat 拼接字符串

select concat(last_name,'_',first_name) 姓名 from employees;

 

3.upperlower

select upper('john');

select lower('john');

 

示例:将姓变大写,名变小写,然后拼接

select concat(upper(last_name),lower(first_name))  姓名 from employees;

 

4.substrsubstring

注意:索引从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('aa' from 'aaaaaaaaa中aaaaaaaaaaaa国人aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')  as out_put;

 

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

select lpad('中国人',2,'!') as out_put;

 

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

select rpad('中国人',12,'aa') 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取余

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-2','%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/xxxx)

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函数的使用一: 

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 

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;