目录
1.字符函数
- concat(str1,str2,...)字符串拼接
//模糊查询
SELECT id,age,name FROM user
WHERE name LIKE CONCAT('%',#{name},'%')
//多字段拼接 18-老王
SELECT CONCAT(age,'-',name) AS newName FROM user
-
upper()、lower()将字符所有字母变大写和小写
select upper('upper') as 大写;
select lower('LOWER') as 小写;
-
substr(str,start,len)截取
str为输入字符串,从start位置开始截取字符串,len表示要截取的长度; 没有指定len长度:表示从start开始起,截取到字符串末尾
//关注我不迷路
select substr('好好学习天天向上关注我不迷路',9,14) as str;
-
instr(str,要查找的子串)函数
返回子串第一次出现的索引,如果找不到,返回0;
// index 8
select instr('一个努力成长的咸鱼','咸鱼') as index;
- trim(str)函数
去掉字符串前后的空格; 该函数只能去掉字符串前后的空格,不能去掉字符串中间的空
//一个努力 成长 的咸鱼
select trim(' 一个努力 成长 的咸鱼 ') as 空格去除;
- replace(str,子串,另一个字符串)函数
将字符串str中的字串,替换为另一个字符串
//一个努力成长的咸鱼
select replace(NAME,'闲鱼','咸鱼') as 替换之后 ;
2.数学函数
- round(x,保留位数) 四舍五入
//123.12345435----> 123.123
select round(amount,3) from payOrder
-
ceil(x) 向上取整 floor(x)向下取整
ceil(x) 向上取整,返回>=该参数的最小整数。求的是大于等于这个数字的最小整数
floor(x) 向下取整,返回<=该参数的最大整数,求的是小于等于这个数字的最大整数。
//1.9--->2 //1.9--->1
select ceil(1.9) as out_put; select floor(1.9) as out_put;
//1.1--->2 //1.1--->1
select ceil(1.1) as out_put; select floor(1.1) as out_put;
//-1.1--->-1 //-1.1--->-2
select ceil(-1.1) as out_put; select floor(-1.1) as out_put;
3.时间与日期函数
- now()函数 curtime()函数
now() 返回当前系统的日期和时间
curtime() 只返回当前的时间,不包含日期
//2023-09-03 21:07:28 //21:07:28
select now() as 当前时间; select curtime() as 当前时间;
- date_format()函数
将日期转换成日期字符串
//2023年09月03日
select date_format('2023/9/3','%Y年%m月%d日') as 转换格式
- 获取日期和时间中的年、月、日、时、分、秒
获取年份: year() 获取月份: month()
获取日: day() 获取小时: hour()
获取分钟: minute() 获取秒数: second()
- last_day()函数
提取某个月最后一天的日期
4.流程控制函数
- if(expr,v1,v2)函数
实现if-else的效果,如果expr是true,返回v1。如果expr是false,返回v2
select name,score, if(score>=60,'合格','不合格') as is qualified from student
- ifnull()函数
判断值是否为null,是null用指定值填充;如果v1不为NULL,返回v2。否则返回v1
5.聚合函数
聚合函数用作统计使用,一般是对多个值进行运算得出一个结果。
sum()求和、avg()求平均、max()求最大、min()求最小、count计算个数