|
概念:类似于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(&#39;john&#39;);
select length(&#39;张三丰hahaha&#39;);
show variables like &#39;%char%&#39;2. concat 拼接字符串
select concat(last_name, &#39;_&#39;, first_name) as 姓名 from employees;3. upper, lower
select upper(&#39;john&#39;);
select lower(&#39;joHn&#39;);示例:将姓变大写,名变小写,然后拼接
select concat(upper(last_name), lower(first_name)) as 姓名 from employees;4. substr, substring
注意:索引从1开始
截取从指定索引处后面所有字符
select substr(&#39;李莫愁爱上了陆展元&#39;, 7) as out_put;截取从指定索引处指定字符长度的字符
select substr(&#39;李莫愁爱上了陆展元&#39;, 1, 3) as out_put;案例:姓名中首字母大写,其他字符小写然后用_拼接,显示出来
select concat(upper(substr(last_name, 1, 1)), &#39;_&#39;, lower(substr(last_name, 2))) from employees;5. instr 返回字串第一次出现的索引,如果找不回返回0
select instr(&#39;杨不悔爱上了殷六侠&#39;, &#39;殷六侠&#39;) as out_put; 6. trim
select length(trim(&#39; 张翠山 &#39;)) as out_put;
select trim(&#39;a&#39; from &#39;aaaaaa张aaaaaa翠山aaaaaa&#39;) as out_put;7. lpad 用指定的字符实现左填充指定长度
select lpad(&#39;殷素素&#39;, 10, &#39;*&#39;) as out_put;8. rpad 用指定的字符实现左填充指定长度
select rpad(&#39;殷素素&#39;, 12, &#39;ab&#39;) as out_put;9. replace
select replace(&#39;张无忌爱上了周芷若&#39;, &#39;周芷若&#39;, &#39;赵敏&#39;) 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(&#39;1998-1-1&#39;) 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(&#39;1988-3-2&#39;, &#39;%Y-%c-%d&#39;) as out_put;查询入职日期为1992-4-3的员工日期
select * from employees where hiredate = &#39;1992-4-3&#39;;
select * from employees where hiredate = str_to_date(&#39;4-3 1992&#39;, &#39;%c-%d %Y&#39;);date_format 将日期转换成字符
select date_format(now(), &#39;%y年%m月%d日&#39;) as out_put;查询有奖金的员工名和入职日期(xx月/xx月 xx年)
select last_name, date_format(hiredate, &#39;%m月/%d日 %y年&#39;) as 入职日期 from employees where commission_pct is not null;
四、其他函数
select version();
select database();
select user();五、流程控制函数
1. if函数:if else的效果
select if(10>5, &#39;大&#39;, &#39;小&#39;);
select last_name, commission_pct, if(commission_pct is null, &#39;没奖金呵呵&#39;, &#39;有奖金嘻嘻&#39;) 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 &#39;A&#39;
when salary>15000 then &#39;B&#39;
when salary>10000 then &#39;C&#39;
else &#39;D&#39;
end as 新工资情况 from employees; |
|