一:MySQL函数—聚合函数
在MySQL中,聚合函数主要有:count,sum,min,max,avg;这些聚合函数我们之前已经说过了,这里就不在重复了。
这里我们学习另外一个函数:group_concat(),该函数用于实现行的合并。
格式:group_concat ([distinct] 字段名 [order by 排序字段 asc/desc] [separator '分隔符'])
现拥有一张表,表名emp
-- 1.将所有员工名字合成一行
select group_concat (emp_name) from emp ;分隔符默认是逗号
-- 2.指定分隔符合并
select group_concat (emp_name separator ';') from emp; 指定用分号隔开
-- 3.指定排序方式和分隔符
select group_concat (emp_name order by salary desc separator ';') from emp group by department;
排完后如下表:
二:MySQL函数—数学函数
-- 1.求绝对值
select abs (-10); // 10
-- 2.向上取整
select ceil (1.1); //2
select ceil (1.0); //1
-- 3.向下取整
select floor (1.9);//1
-- 4.取列表最大值
select greatest (1,2,3); //3
-- 5.取列表最小值
select least (1,2,3);//1
-- 6.取余
select mod (5,2); //1
-- 7.取x的y次方
select power (2,3); //8
-- 8.取随机数
select rand ();
-- 9.将小数四舍五入取整
select round (3.5415); //4
-- 将小数指定位数后四舍五入
select round (3.1415,3); //3.142
-- 10.将小数直接截取到指定位数
select truncate (3.1415,3); //3.141
这些只是简单的计算,后面可以结合表达式使用,括号里可以是字段表达式
三:MySQL函数—字符串函数
-- 1.获取字符串字符个数
select char_length ('hello'); //5
select char_length ('你好啊');//3
-- length 取长度,返回的单位是字节
select length ('你好啊'); // 9 UTF-8 一个汉字占3个字节
-- 2.字符串合并
select concat ('hello','world'); //helloworld
-- 2.指定分割符合并
select concat_ws ('-','hello','world'); //hello-world
-- 3.返回字符串在列表中第一次出现的位置
select field ('aaa','aaa','bbb','ccc'); //1
select field ('bbb','aaa','bbb','ccc'); //2
-- 4.去除字符串左边空格
select ltrim (' aaa'); //aaa
去除右边的 rtrim 去除左右的 trim
-- 5.字符串截取
select mid ('helloworld',2,3); //ell 从第二个字符开始截取,截取长度为3
-- 6.获取字符串A在字符串中的位置
select position ('abc' in 'habcelloworld'); //2
-- 7.字符串替换
select replace ('aaahelloaaaworld','aaa','bbb'); //bbbhellobbbworld
-- 8.字符串翻转
select reverse ('hello'); //olleh
-- 9.返回字符串的后几个字符
select right ('hello',3); //llo
-- 10.字符串比较
select strcmp ('hello','world'); //-1 说明后面的大,比较是一个字母一个字母比较h<w
-- 11.字符串截取
select substr ('hello',2,3); //ell 从第二个字符开始截取,截取三个字符
select substring ('hello',2,3); //ell 同上
-- 12.将小写转大写
select ucase ('hello'); //HELLO
select upper ('hello');
-- 13.将大写转小写
select lcase ('HELLO'); //hello
select lower ('HELLO');
以上函数都可以在表格查询时灵活运用
四:MySQL函数—日期函数
-- 1.获取时间戳(毫秒值)返回从1970-01-01 00:00:00 到当前毫秒值
select unix_timestamp ();
-- 2.将一个日期转换成毫秒值
select unix_timestamp('2021-12-21 08:08:08'); //1640045288
-- 3.将时间戳毫秒值转换成指定的日期
select from_unixtime(1640045288,'%Y-%m-%d %H-%i-%s'); //2021-12-21 08:08:08'
-- 4.获取当前的年月日
select curdata ();
select current_data();
-- 5.获取当前的时分秒
select curtime ();
select current_time();
-- 6.获取当前的年月日时分秒
select current_timestamp ();
-- 7.从日期字符串中获取年月日
select data ('202-12-12 12:34:56'); //2022-12-12
-- 8.获取日期之间的差值
select datediff ('2021-12-23','2008-08-08');//4885
-- 9.获取日期之间的差值(秒级)
select timediff ('12:12:34','10:18:56'); //01:53:38
-- 10.日期格式化
select date_format ('2021-1-1 1:1:1','%Y-%m-%d %H:%i:%s'); //2021-01-01 01:01:01
-- 11.将字符串转换成日期
select str_to_date ('2021-12-13 11:11:11','%Y-%m-%d %H:%i:%s'); //2021-12-13 11:11:11
-- 12.将日期进行减法(日期向前跳转)day可以换成month,year之类的
select date_sub ('2021-10-01', interval 2 day); // 2021-09-30
-- 13.将日期进行加法
select date_add ('2021-10-01',interval 2 day);
-- 14.从日期中获取小时 同样的hour可以灵活改变
select extract (hour from '2021-12-13 11:12:13'); //11
-- 15.获取给定日期的最后一天
select last_day ('2021-08-12'); //2021-12-31
-- 16.获取指定年份和天数的日期
select makedate ('2021',53); //2021-02-22
-- 17.根据日期获取年月日,时分秒 同理minute可以自由替换
select minute ('2021-12-13 11:12:13'); //12
-- 18.根据日期获取信息
select monthname ('2021-12-13 11:12:13'); //December 获取月份的英文
select dayname ('2021-12-13 11:12:13'); //Monday 获取周几
select dayofmonth ('2021-12-13 11:12:13'); //13 当月的第几天
select dayofweek ('2021-12-13 11:12:13'); //2 1:周日 2:周一 以此类推
select dayofyear ('2021-12-13 11:12:13'); //347 获取一年的第几天
......日期函数很多,不用死记,用的时候查‘就行