IE盒子

搜索
查看: 113|回复: 0

MySQL_基础4.1_常见函数_单行函数

[复制链接]

2

主题

4

帖子

8

积分

新手上路

Rank: 1

积分
8
发表于 2023-3-25 19:23:24 | 显示全部楼层 |阅读模式
概念:类似于java的方法,将一组逻辑语句封装在方法体中,对外暴露方法名
优点:

  • 隐藏了实现细节
  • 提高了代码的重用性
调用:select 函数名(实参列表) 【from 表】;
特点:

  • 叫什么(函数名)
  • 干涉么(函数功能)
分类:

  • 单行函数_常见函数


  • 字符函数:length, concat, substr, instr, trim, upper, lower, lpad, rlpd, 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
2. 分组函数

  • 功能:做统计使用,又称为统计函数、聚合函数、组函数
<hr/>一、字符函数
1. length 获取参数值的字节个数
select length('john');
select length('张三丰hahaha');
show variables like '%char%'2. concat 拼接字符串
select concat(last_name, '_', first_name) as 姓名 from employees;3. 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(last_name, 2))) from employees;5. instr 返回字串第一次出现的索引,如果找不回返回0
select instr('杨不悔爱上了殷六侠', '殷六侠') as out_put; 6. trim
select length(trim('    张翠山    ')) as out_put;
select trim('a' from 'aaaaaa张aaaaaa翠山aaaaaa') as out_put;7. lpad 用指定的字符实现左填充指定长度
select lpad('殷素素', 10, '*') as out_put;8. rpad 用指定的字符实现左填充指定长度
select rpad('殷素素', 12, 'ab') as out_put;9. replace
select replace('张无忌爱上了周芷若', '周芷若', '赵敏') as out_put;二、数学函数
round 四舍五入
select round(1.65);
select round(-1.65);
select round(1.567, 2);ceil 向上取整,返回大于等于等于该参数的最小整数
select ceil(1.002);
select ceil(-1.002);floor 向下取证,返回<=该参数的最大整数
select floor(-9.99);truncate 截断
select truncate(1.69, 1);mod 取余 mod(a,b): a-a/b*b
select mod(10,3);
select 10%3;
select mod(-10,3);
select 10%3;三、日期函数
now 返回当前系统日期+时间
select now();curdate 返回当前系统日期,不包含时间
select curdate();curtime 返回当前时间,不包含日期
select curtime();可以获取知道你过的部分,年、粤、日、小时、分钟、秒
select year(now()) as 年;
select year('1998-1-1') as 年;
select year(hiredate) as 年 from employees;

select month(now()) as 月;
select monthname(now()) as 月;

select day(now());
select hour(now());
select minute(now());
select second(now());
str_to_date 将字符通过指定的格式转换成日期
select str_to_date('1988-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月/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 备注;2. case函数的使用一:switch case的效果
mysql中
case 要判断的字段或表达式
when 常量1 then 要显示的值1或语句1
when 常量2 then 要显示的值2或语句2
when 常量3 then 要显示的值3或语句3
...
else 要显示的值n或语句n
end 案例:查询员工的工资,要求
部门号=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
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;
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表