mysql常用函数
- 1. 第一类:数值函数
 - 1.1 圆周率pi的值
 - 1.2 求绝对值
 - 1.3 返回数字的符号
 - 1.4 开平方,根号
 - 1.5 求两个数的余数
 - 1.6 截取正数部分
 - 1.7 向上取整数
 - 1.8 向下取整数
 - 1.9 四舍五入函数
 - 1.10 随机数函数
 - 1.11 数值左边补位函数
 - 1.12 数值右边补位函数
 - 1.13 次方函数
 - 1.14 特殊次方函数e
 - 1.15 对数函数
 - 1.16 其他对数函数
 - 1.17 三角函数
 
- 2. 第二类:字符串函数
 - 2.1 计算字符串的长度
 - 2.2 字符串大小写转换
 - 2.3 字符串反转
 - 2.4 字符串合并
 - 2.5 删除空格
 - 2.6 取左右两边的字符串
 - 2.7 取中间的字符串
 - 2.8 返回字符串的位置
 - 2.9 字符串的替换
 - 2.10 比较字符串
 - 2.11 重复字符串
 
- 3. 第三类:时间日期函数
 - 3.1 获取当前的日期
 - 3.2 获取当前的时间
 - 3.3 获取当前的日期时间
 - 3.4 获取unix时间戳
 - 3.5 时间戳转为日期
 - 3.6 获取年月日时分秒季度星期
 - 3.7 时间转换为秒
 - 3.8 秒转换为时间
 - 3.9 时间日期的加减
 - 3.10 两个日期相减
 - 3.11 日期格式化
 
- 4. 第四类:聚类函数
 - 5. 第五类:逻辑判断函数
 - 5.1. if
 - 5.2 ifnull
 - 5.3 case when
 
1. 第一类:数值函数
1.1 圆周率pi的值
select pi();
 

1.2 求绝对值
- abs(x) 
  
- 参数x就是一个数值
 
 
select abs(1),abs(-11);
 

1.3 返回数字的符号
- sign(x),x为数值 
  
- 正数返回1; 负数返回-1; 0返回0
select sign(1),sign(-2.34),sign(11),sign(0); 
 - 正数返回1; 负数返回-1; 0返回0
 

1.4 开平方,根号
- sqrt(x),x是一个数值
select sqrt(2);
 
1.5 求两个数的余数
- mod(x,y) 
  
- x除数,y被除数
 
select 9/7,mod(9,7) -- 余数;
 
1.6 截取正数部分
- truncate(x,n),n截取的小数的位数
 - 注意:n可以是负数,意思是-1是各位,-2是十位…
select 9/7,truncate(9/7,0),truncate(9/7,2),341243552352,truncate(341243552352,-2);
 
1.7 向上取整数
- 向上取整数,如果有小数的话,向上进一位,值变大
 - ceil(x),ceiling(x)
select 12.43434,ceil(12.43434),ceil(-12.43434),ceiling(12.43434);
 
1.8 向下取整数
- 向下取整数,把小数去掉
 - floor(x)
select 12.43434,floor(12.43434),floor(-12.43434);
 
1.9 四舍五入函数
- round(x,n),n是四舍五入的位数
 - 注意:n可以为负数,-1代表个位,-2代表十位…取0
select 12.4224,round(12.4224),round(12.4224,2),round(12.4224,-2);
 
1.10 随机数函数
- 产生一个随机数
 - rand(x),0-1之前的一个随机数 
  
- x是一个数值,如果加这个参数的话,就代表每次产生额随机种子是一样的
 
select rand();
 
1.11 数值左边补位函数
- left : lpad(x,n,符号)左边补
 - x:数值,n:补几位,符号:补什么符号
select lpad(1,4,0),lpad(2,4,0);
 
1.12 数值右边补位函数
- right : rpad(x,n,符号)右边补
 - x:数值,n:补几位,符号:补什么符号
select rpad(1,4,0),rpad(2,4,'x');
 
1.13 次方函数
- pow(x,n), 
      
       
        
         
         
           x 
          
         
           n 
          
         
        
       
         x^n 
        
       
     xn
select pow(2,3);
 
1.14 特殊次方函数e
- exp(x) 
  
- x是几次方的参数
 
-- 自然数e,2.7182818.... select exp(1);
 
1.15 对数函数
- ln(x),自然数e的对数
select ln(exp(1));
 
1.16 其他对数函数
- log(x)
select log(10,1000),round(log(10,1000));
 
1.17 三角函数
- 三角函数:sin,cos,tan,cot…
select sin(pi()/6),cos(pi()/6),tan(pi()/6),cot(pi()/6);
 
2. 第二类:字符串函数
2.1 计算字符串的长度
- length(x),x字符串
 - 注意:一个中文汉字占3个字节
select "asjncsdijcdic",length("asjncsdijcdic"),length("你好");
 
2.2 字符串大小写转换
- lower(x),全部转为小写
 - upper(x),全部转为大写
 - x为字符串
select "HEllo",lower("HEllo"),upper("HEllo"); ```  
2.3 字符串反转
- reverse(x)
select reverse('abc');
 
2.4 字符串合并
-  
就是把字符串连在一起
 -  
concat(s1,s2,s3…)
select concat('he','el','lo','你好');
 -  
concat_ws(分隔符,s1,s2,s3…),指定分隔符连接
select concat_ws('-','hello','world');
 
2.5 删除空格
- 删除字符串左右两边多余的空格
 - trim(x),删除左右两边的空格
 - ltrim(x),删除左边的空格
 - rtrim(x),删除右边的空格 
  
- x为字符串
 
select " 你好 hello world ",trim(" 你好 hello world ") trim,ltrim(" 你好 hello world ") ltrim,rtrim(" 你好 hello world ") rtrim;
 
2.6 取左右两边的字符串
- 左边取: left(x,n),x字符串,n取几位
 - 右边取: right(x,n),x字符串,n取几位
select "helloworld",left("helloworld",5) lef,right("helloworld",5) rig;
 
2.7 取中间的字符串
- substring(x,n,m) 
  
- x字符串,n从第几位开始取,m取几位
 
select "hello world yes", substring("hello world yes",7,5), substring("hello world yes",7);
 
2.8 返回字符串的位置
- locate(子字符串,字符串)
 - position(子字符串 in 字符串)
select locate("world","hello world yes"),position("world" in "hello world yes");
 
2.9 字符串的替换
-  
replace(字符串,要替换的内容,替换的内容)
select replace("hello world yes","world","世界");
 -  
insert(字符串,替换的开始位数,取机位,替换的字符串)
select insert("hello world yes",7,5,"世界呀");
 
2.10 比较字符串
- 比较两个字符串是不是一样,1代表不一样,0代表一样
 - strcmp(s1,s2)
select strcmp('hello','Hello'),strcmp('hello','hello'),strcmp('hello ','Hello');
 
2.11 重复字符串
- repeat(x,n),x字符串,n重复的次数
select repeat('hello',3);
 
3. 第三类:时间日期函数
3.1 获取当前的日期
- current_date()
 - 注意:current_date可以简写为curdate
select current_date(),curdate();
 
3.2 获取当前的时间
- current_time()
 - 注意:current_time可以简写为curtime
select current_time(),curtime();
 
3.3 获取当前的日期时间
- now()
 - 注意:now可以写为sysdate()
select now(),sysdate();
 
3.4 获取unix时间戳
- 时间戳就是从1970-1-1开始到现在过了多少秒
select unix_timestamp(now()),format(unix_timestamp(now()),0);
 
3.5 时间戳转为日期
- from_unixtime(x),x时间戳
select from_unixtime(unix_timestamp(now()));
 
3.6 获取年月日时分秒季度星期
-  
年:year(date)
select now(),year(now())
 -  
月:
- month(date)
 - monthname(date),获取月的英文名
select now(),month(now()),monthname(now())
 
 -  
星期:
- weekday(date),星期一:0…星期日:6
 - dayofweek(date),星期一:0…星期日:1
 - dayname(date),获取星期的英文名
select weekday(now()),dayofweek(now()),dayname(now())
 
 -  
获取当前的周数,一般一年有53周
- week(date),weekofyear(date)
select now(),week(now()),weekofyear(now())
 
 - week(date),weekofyear(date)
 -  
获取当前过了多少天
- dayofyear(date)
select now(),dayofyear(now())
 
 - dayofyear(date)
 -  
获取本月的天
- day(date),dayofmonth(date)
select now(),day(now()),dayofmonth(now())
 
 - day(date),dayofmonth(date)
 -  
获取分钟:minute(date)
 -  
获取小时:hour(date)
 -  
获取季度:quarter(date)
 -  
获取秒:second(date)
select now(),minute(now()) ,hour(now()),quarter(now()),second(now())
 
3.7 时间转换为秒
- time_to_sec()
select time_to_sec('5:28:33');
 
3.8 秒转换为时间
- sec_to_time()
select sec_to_time(19713);
 
3.9 时间日期的加减
-  
加日期:
-  
date_add(date,interval 数字【year/month/hour/minute/second…】)
- interval 数字 [day_hour/day_minute/day_second/year_month/year_second]
 
 -  
adddate(date,interval 数字 【year/month/hour/…】)
select date_add(now(),interval 1 day),adddate(now(),interval 1 day);
-- 加一天五小时33分钟23秒 select date_add(now(),interval 1 day),date_add(now(),interval "1 5:33:23" day_second);
 
 -  
 -  
减日期:
- date_sub(date,interval 数字 【year/month/hour/minute/second…】) 
    
- interval 数字[day_hour/day_minute/day_second/year_month/year_second]
 
select now(), date_sub(now(),interval 1 day);
 
 - date_sub(date,interval 数字 【year/month/hour/minute/second…】) 
    
 -  
时间加减
- addtime(),time_add()
 - subtime(),time_sub()
 
 -  
inerval 关键词
select '2024-7-18' + interval 1 day,'2024-7-18' + interval '2 2' day_hour,'2024-7-18' - interval '2 2' day_hour
 
3.10 两个日期相减
- datediff(日期1,日期2),得到的是天数
select datediff('2024-1-30','2024-1-27 7:23:34');
 
3.11 日期格式化
- date_format(date,格式化形式)
select date_format(now(),"%Y%m");
 
4. 第四类:聚类函数
| 函数 | 含义 | 
|---|---|
| min() | 最小值 | 
| max() | 最大值 | 
| avg() | 平均值 | 
| sum() | 求和 | 
| count() | 计数 | 
select 
	c_id,
	max(s_score) as max_score,
	min(s_score) as min_score,
	avg(s_score) as avg_score,
  sum(s_score) as sum_score,
	count(s_score) as count_score
from 
	score
group by
	c_id
 

5. 第五类:逻辑判断函数
5.1. if
- if(判断,判断成立返回的值,不成立返回的值)
SELECT a.*, if(a.s_score>=60,"及格","不及格") FROM score a
 
5.2 ifnull
- ifnull(x,y),x为空时返回y,x不为空返回x本身
 - 例1
select ifnull(null,100);
 - 例二
SELECT a.*, ifnull(b.s_score,0) FROM student a LEFT JOIN score b ON a.s_id=b.s_id;
 
5.3 case when
- 语法格式
case when 判断 then 判断成立返回的值 when 判断 then 判断成立返回的值 when 判断 then 判断成立返回的值 when 判断 then 判断成立返回的值 ... else 判断成立返回的值 end - 例子
SELECT a.*, case when a.s_score<60 then "不及格" when a.s_score<80 then "中等" ELSE "优秀" end as 成绩分组 FROM score a; 




















