概述:
类似于Java的方法,将一组逻辑语句封装在方法体中。
1、隐藏了实现细节
2、提高了代码的重用性
1、length()函数
# length
select length('jjjjjjlloi');
select length("林俊杰") -- 9个字节 一个汉字三个字节
2、concat()函数
-- concat
select concat('hell','_','world'); -- hell_world
3、instr()函数
-- instr
select instr('agffsdga','a'); -- 1
4、trim ()函数
-- trim 去掉前后空格
select length(trim(' 好家伙 ')); -- 9
select trim('a' from 'aaaaaaaaaaaaaaaa好aaaaaaaaa家伙aaaaaa'); -- 好aaaaaaaaa家伙
5、lpad()函数
-- lpad 左填充
select lpad('小猫咪',10,'*'); -- *******小猫咪
6、rpad()函数
-- rpad 右填充
select rpad('小而亡', 20,'*');
7、replace()函数
-- replace
select replace('小王小王小王小王小王小明校长效力','小王','零零');
-- 零零零零零零零零零零小明校长效力
1、round()函数
select round(1.45); -- 1
select round(-1.45); -- -1
select round(1.3445,2); -- 1.34
2、ceil()向上取整函数
-- ceil 向上取整 返回>=
select ceil(-1.02); -- -1
3、floor()向下取整函数
-- floor 向下取整 返回<=
select floor(-9.99); -- -10
4、truncate()截断函数
-- truncate 截断
select truncate(1.33334,1);
-- 1.3
5、mod()取余函数
-- mod 取余
-- mod(a,b) : a-a/b*b
select mod(10,-3); -- 1
1、now ()函数
-- now 返回当前系统日期+时间
select now();
2、curdate () 返回当前时间,不含时间
-- curdate 返回当前时间,不含日期
select curdate();
3、year()、month()、day()、hour()…等函数可以获取指定的部分,年,月,日,小时,分钟, 秒
-- 可以获取指定的部分,年、月、日、小时、分钟、秒
select year(now()); -- 2021
select month(now()); -- 8
select year('1999-9-9'); -- 1999
select monthname(now()); -- August
4、str_to_date ()函数 将日期格式的字符转换成日期
-- str_to_date:将日期格式的字符转换成日期
select str_to_date('1996-3-2','%Y-%c-%d'); -- 1996-03-02
select str_to_date('4-3 1880','%c-%d %Y'); -- 1880-04-03
5、date_format ()函数 将日期转换成字符
-- date_format 将日期转换成字符
select date_format(now(),'%Y年%c月%d日'); -- 2021年8月04日
1、if() 函数
select if(10>7,'大','小'); -- 大
2、switch ()函数
-- case 相当于java中 switch case
-- switch(变量表达式){
-- case 常量1:语句1;
-- break
-- ....
-- default :语句n;
-- break;
-- }
--
-- mysql中:
-- case 要判断的字段或表达式
-- when 常量1 then 要显示的值1或者语句1
-- when 常量2 then 要显示的值2或者语句2
-- .....
-- else 要显示的值n或者语句n;
-- end
select salary
case
when salary>2000 then 'a'
when salary>1500 then 'b'
when salary>1000 then 'c'
else 'd'
end
from pople
select version(); -- 显示版本号
select database(); -- 显示数据库
select user(); --显示用户名
cs