学习笔记-mysql-各种函数的基本使用

1. 聚合函数

count , sum , min , max ,avg , group_concat()

-- 将所有员工的名字合并成一行 
select group_concat(emp_name) from emp;
-- 指定分隔符合并 
select department,group_concat(emp_name separator ';' ) from emp group by department; 
-- 指定排序方式和分隔符 
select department,group_concat(emp_name order by salary desc separator ';' ) from emp group by department;
-- 附上 sql server 的写法  比较
select department,STRING_AGG(emp_name ,';') from emp group by department

2. 数学函数

-- 绝对值
select abs(-10)  --返回10
-- 向上取整
select ceil(1.1);  --返回2
select ceil(1.0);  --返回1
-- 向下取整
select floor(1.1);  --返回1
select floor(1.9);  --返回1
-- 返回列表中的最大值
select greatest(1,2,3);  --3
select least(1,2,3);  --1
-- 取模 求余数
select mod(5,2) --1
-- x的y次方
select power(2,3)  --8
-- 取随机数
select rand()  --随机数(0到1)
select floor(rand() * 100 ) --100以内随机数
-- 将小数四舍五入取整
select round(3.5345)  --4
select round(3.5345,3)  --3.535
-- 将小数直接截取到指定位数
select truncate(3.1415,3)  --3.141

3. 字符串函数

-- 获取字符串长度
select char_length('hello'); --5
select char_length('你好吗');  --3
select length('hello'); --5
select length('你好吗');  --9  length取长度返回的是字节
-- 字符串合并
select concat('hello','world');  -- helloworld 无分隔符
select concat_ws('-','hello','world'); -- hello-world 有分隔符
-- 返回字符串在列表中第一次出现的位置
select field('aa','aa','bb','cc');  --1
select field('bb','aa','bb','cc');  --2
-- 去除字符串空格
select ltrim('  aaaa');
select rtrim('aaaa    ');
select trim('  aaaa    ');
-- 字符串截取
select mid('helloworld',2,3);  --ell
select substr('helloworld',2,3);  --ell
select substring('helloworld',2,3);  --ell
-- 获取字符串a在字符串b中的位置
select position('abc' in 'habchelloworld') --2
-- 字符串替换
select replace('habchelloworld','habc','') --helloworld
-- 字符串翻转
select reverse('hello') --olleh
-- 返回字符串的后几个字符
select right('hello',2)  --lo
-- 小写转大写
select ucase('hello');
select upper('hello');
-- 大写转小写
select lcase('Hello');
select lower('Hello');

4. 日期函数

-- 获取时间戳(毫秒值) 返回从1970-01-01 00:00:00到当前毫秒值
select unix_timestamp()
-- 将指定的时间转为毫秒时间戳
select unix_timestamp('2023-11-11 11:11:11')
-- 将时间戳毫秒值转为指定的时间格式
select from_unixtime(1598079966,'%Y-%m-%d %H:%i:%s');
-- 获取当前日期
select curdate() --年月日
select current_date()  --年月日
select current_time()  --时分秒
select curtime()  --时分秒
select current_timestamp() ;-- 年月日时分秒
select now() -- 年月日时分秒
-- 从具体时间中获取年月日
select date('2023-11-11 11:11:11')  --2023-11-11
-- 获取日期之间的差值
select datediff('2023-11-11','2023-11-1')  --10
-- 获取时间的差值(秒级)
select timediff('8:40:00','12:00:00') 
-- 日期格式化
select date_format('2023-1-1 1:1:1','%Y-%m-%d %H:%i:%s') --2023-01-01 01:01:01
-- 将字符串转为日期
select str_to_date('2023-1-1 1:1:1','%Y-%m-%d %H:%i:%s'); --2023-01-01 01:01:01
SELECT str_to_date("August 10 2017", "%M %d %Y") --2017-08-10
-- 将日期进行加减
select date_sub('2023-11-11',interval 2 day) --2023-11-09
select date_add('2023-11-11',interval 2 month) --2024-01-11
-- 从日期中获取 年|月|日|时|分|秒..
select extract(year from '2023-11-11')
select day('2023-05-01 11:22:33')
select month('2023-05-01 11:22:33')
select quarter('2023-05-01 11:22:33')  --2 季度
select monthname('2023-05-01 11:22:33') -- may
select dayname('2023-05-01 11:22:33') -- Monday 周几
select dayofweek('2023-05-01 11:22:33') -- 2 这周的第几天
select dayofmonth('2023-05-01 11:22:33') -- 1 这个月的第几天
select dayofyear('2023-05-01 11:22:33') -- 121 这年的第几天
select week('2023-05-01 11:22:33') -- 18 这年的第几周
-- 获取给定日期所在月的最后一天
select last_day('2023-11-11') --2023-11-30
--获取指定年份和天数的日期
select makedate('2023',53) --2023-02-22

5. 控制流函数

(1). if逻辑判断
-- if(expr,v1,v2)  表达式expr成立返回v1,否则返回v2
select if(score>80,'优秀','及格') flag ,* from score 
-- ifnull 如果表达式时null,转换显示为指定值
select ifnull(5,0); -- 5
select ifnull(null,0); -- 0
-- isnull 判断表达式是否为null
select isnull(5); -- 0
select isnull(null); -- 1
-- nullif(expr1,expr2) 判断两个字符串是否相同,相同返回null,不同返回expr1
select nullif(12,12); -- null
select nullif(12,13); -- 12
注意:在sql server 中isnull()的用法与mysql中的ifnull用法一致,没有ifnull
(2). case when

6. 窗口函数

mysql 8.0之后增加的,也称为开窗函数

(1). 序号函数
  • row_number( ) --排序 1,2,3
  • rank( ) --排序 1,1,3
  • dense_rank( ) --排序 1,1,2
  • 另外还有开窗聚合函数:sum avg min max
-- 格式
row_number()|rank()|dense_rank() over ( 
  partition by ... 
  order by ... 
) 

(2). 分布函数
  • cume_dist()
    用途:分组内小于、等于当前rank值的行数 / 分组内总行数
-- 查询小于等于当前薪资
select  dname,ename,salary,
cume_dist() over(order by salary) as rn1, -- 没有partition语句 所有的数据位于一组
cume_dist() over(partition by dept order by salary) as rn2 
from employee;

  • percent-rank()
    用途:每行按照公式(rank-1) / (rows-1)进行计算。其中,rank为RANK()函数产生的序号,rows为当前窗口的记录总行数
-- 
select dname,ename,salary,
rank() over(partition by dname order by salary desc ) as rn,
percent_rank() over(partition by dname order by salary desc ) as rn2
from employee;

(3). 前后函数
  • lag(expr,n,x)
    用途:返回位于当前行的前n行(lag(expr,n))或后n行(LEAD(expr,n))的expr的值
-- last_1_time 查询排序前1名职员的入职时间
-- last_2_time 查询排序前2名职员的入职时间
-- '2000-01-01'分组的第一个值没有前一行,所以设置一个默认值,可不写,返回null
select dname,ename,hiredate,salary,
lag(hiredate,1,'2000-01-01') over(partition by dname order by hiredate) as last_1_time,
lag(hiredate,2) over(partition by dname order by hiredate) as last_2_time 
from employee;
  • lead(expr,n,x)
    用途:返回位于当前行的后n行
-- last_1_time 查询排序后1名职员的入职时间
-- last_2_time 查询排序后2名职员的入职时间
-- '2000-01-01'分组的第一个值没有前一行,所以设置一个默认值,可不写,返回null
select dname,ename,hiredate,salary,
lead(hiredate,1,'2000-01-01') over(partition by dname order by hiredate) as last_1_time,
lead(hiredate,2) over(partition by dname order by hiredate) as last_2_time 
from employee;

(4). 头尾函数
  • first_value | last_value
    用途:first_value(expr) 到目前为止的排序第一的
    last_value(expr) 到目前为止的最后一个,实际上就是本行的值
-- 截止到当前,按照日期排序查询第1个入职和最后1个入职员工的薪资
-- 注意,  如果不指定ORDER BY,则进行排序混乱,会出现错误的结果
selectdname,ename,hiredate,salary,
first_value(salary) over(partition by dname order by hiredate) as first,
last_value(salary) over(partition by dname order by  hiredate) as last 
from  employee;
(5). 其他函数
  • nth_value(expr,n)
    用途:返回窗口中第n个expr的值。expr可以是表达式,也可以是列名
--截止到当前薪资,显示每个员工的薪资中排名第2或者第3的薪资
select  dname,ename,hiredate,salary,
nth_value(salary,2) over(partition by dname order by hiredate) as second_score,
nth_value(salary,3) over(partition by dname order by hiredate) as third_score
from employee
  • ntile(n)
    用途:将分区中的有序数据分为n个等级,记录等级数
-- 根据入职日期将每个部门的员工分成3组
select dname,ename,hiredate,salary,
ntile(3) over(partition by dname order by  hiredate  ) as rn 
from employee;

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>