mysql常见函数

常见函数

功能: 类似于Java的方法,将一组逻辑语句封装在方法体中,对外暴露方法名

好处:1、隐藏了实现细节  2、提高代码的重用性

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

特点:
     1. 叫什么 (函数名)
     2. 干什么 (函数功能)
     
分类:
     1. 单行函数
        如:contact、length、 ifnull等
        
     2. 分组函数
        功能: 做统计使用,又称为统计函数、聚合函数、组函数
        
单行函数  
      1. 字符函数
          length
          concat
          substr
          instr
          trim
          upper
          lower
          lpad
          rpad
          replace
       
      2. 数学函数
         round
         ceil
         floor
         truncate
         mod
         
      3. 日期函数
         now
         curdate
         curtime
         year
         month
         monthname
         day
         hour
         minute
         second
         str_t_date
         date_format
         
      
         
      4. 其他函数
         version
         database
         user
         
      5. 流程控制函数  
         if
         case

一、 字符函数

1. length 获取参数值的字节个数 (utf8:一个字母占一个字节,一个汉字占三个字节)

select length('john');
select length('张三丰hahaha');
show variables like '%char%'; #显示客户端的字符集

2. concat 拼接字符串

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

3. upper、lower (upper 变大写, lower变小写)

select upper('john');
select lower('JOHN');

示例:将姓变大写,名变小写

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

4. substr、substring

注意:索引从1开始

截取从指定索引处后面所有字符

select substr('李莫愁爱上了陆展元',7) as out_put;

截取从指定索引处指定字符长度的字符

select substr('李莫愁爱上了陆展元',1,3) AS out_put;

案例:姓名中首字符大写,其他字符小写然后下划线拼接,显示出来

select concat(upper(substr(last_name,1,1)),'_',lower(substr(first_name,2)))
from employees;

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

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

6. trim 去前后空格 或去掉前后指定的字符

select length(trim(' 张翠山 ')) as out_put;

select Trim('a' from 'aaaa张翠山aaaaa') as out_put;

7. lpad 用指定的字符实现左填充指定长度,原本的字符长度超过指定长度,会截掉超过的字符

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

8. rpad 用指定的字符实现右填充指定长度,原本的字符长度超过指定长度,会截掉超过的字符

SELECT rpad('殷素素',2,'*') AS out_put;

9. replace 替换

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

二、数学函数

1. round 四舍五入

select round(1.45);

小数点之后保留几位

select round(1.567,2);

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

select CEIL(1.0003);
SELECT CEIL(-1.0003);

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

select floor(-9.99)

4. truncate 截断

select truncate(1.6999,1);

5. mod 取余

/* mod(a,b) : a-a/b*b   这里a/b取整
mod(-10,-3):-10-(-10)/(-3)*(-3) = -1
*/

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

三、 日期函数

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

select now();

2. curdate 返回当前系统日期,不包含时间

select curdate();

3. curtime 返回当前系统时间,不包含日期

select curtime();

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

select year(now()) as 年;
SELECT YEAR('1998-1-1') AS 年;

select year(hiredate) as 年 from employees;

select month(now()) 月;
select monthname(now()) 月; #显示英文

5. str_to_date: 将日期格式的字符转换成指定格式的日期

select str_to_date ('9-13-1999','%m-%d-%Y');

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

6. data_format: 将日期转换成字符

select Date_format('2018/6/6','%Y年%m月%d日');

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

select
     last_name,
     date_format(hiredate,'%m月/%d日 %y年') as 入职日期
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,'没奖金,呵呵','有奖金,嘻嘻') as 备注
from
     employees;

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

switch(变量或表达式){
   case 常量1:语句1; break;
   ...
   default:语句n;break;
   }


mysql中

case 要判断的字段或表达式
when 常量1 then 要显示的值1或语句1
when 常量1 then 要显示的值1或语句1
...
else 要显示的值n或语句n;

案例:查询员工的工资,要求
部门号 = 30,显示的工资为1.1 倍
部门号 = 40,显示的工资为1.2 倍
部门号 = 50,显示的工资为1.3 倍
其他部门,显示的工资为原工资

select
      salary as 原始工资,
      `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
when 条件2 then 要显示的值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;

作业

  1. 显示系统时间(注:日期+时间)

select now();

  1. 查询员工号、姓名、工资,以及工资提高百分之20之后的结果
select
     `employee_id`,
     `last_name`,
     `salary`,
     salary*(1+0.2) as new_salary
from
    employees;
  1. 将员工的姓名按首字母排序,并写出姓名的长度
select 
     last_name,
     length(last_name) as 长度
from
    employees
order by 
    substr(last_name,1,1)
asc, 长度 asc;

4.做一个查询,产生下面结果
<last_name> earns monthly but wants <salary*3>
Dream Salary
King earns 24000 monthly but wants 72000

select concat(last_name,'earns',salary,'monthly but wants',salary*3) as 'Dream Salary'
from
employees
where salary = 24000;
  1. 使用case-when,按照下面的条件:
    job grade
    AD_PRES A
    ST_MAN B
    IT_PROG C
    SA_REP D
    ST_CLERK E
select distinct `job_id` from employees;
 
SELECT
      last_name,
     `job_id` as job,
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 Grade
from employees
posted @ 2021-07-05 10:14  突然跳舞的咸鱼  阅读(80)  评论(0)    收藏  举报