(一)数字函数
1、abs(x) 返回x的绝对值
select abs(-1);
-- 返回1
12
2、avg(expression) 返回一个表达式的平均值,expression 是一个字段
select avg(age) from student;
-- 返回表student中age字段下数字的平均值
3、ceil(x) / ceiling(x) 返回大于或等于 x 的最小整数
select ceil(1.5);
select ceiling(1.5);
-- 返回2
4、floor(x) 返回小于或等于 x 的最大整数
select floor(1.5);
-- 返回1
5、exp(x) 返回 e 的 x 次方
select exp(3);
-- 计算 e 的三次方,返回20.085536923188
6、greatest(expr1, expr2, expr3, …) 返回列表中的最大值
select greatest(3, 12, 34, 8, 25);
-- 返回以下数字列表中的最大值34
7、least(expr1, expr2, expr3, …) 返回列表中的最小值
select least(3, 12, 34, 8, 25);
-- 返回以下数字列表中的最小值3
8、ln 返回数字的自然对数
select ln(2);
-- 返回 2 的自然对数:0.6931471805599453
9、log(x) 返回自然对数(以 e 为底的对数)
select log(20.085536923188);
-- 返回 3
10、max(expression)返回字段 expression 中的最大值
select max(age) as maxage from student;
-- age最大值
11、min(expression)返回字段 expression 中的最大值
select min(age) as minage from student;
-- age最小值
12、pow(x,y) / power(x,y)返回 x 的 y 次方
select pow(2,3);
select power(2,3);
-- 返回2 的 3 次方:8
13、rand()返回 0 到 1 的随机数
select rand();
-- 返回 0 到 1 的随机数,若()里面有数字,rand(x),x相同时,返回值相同
14、round(x)返回离 x 最近的整数
select round(1.23456);
-- 返回 1
15、sign(x)返回 x 的符号,x 是负数、0、正数分别返回 -1、0 和 1
select sign(-10);
-- 返回 -1
16、sqrt(x)返回x的平方根
select sqrt(25);
-- 返回5
17、sum(expression)返回指定字段的总和
select sum(age) as totalaage from student;
-- 返回age的总和
18、truncate(x,y)返回数值 x 保留到小数点后 y 位的值(与 round 最大的区别是不会进行四舍五入)
select truncate(1.23456,3);
-- 返回1.234
(二)字符串函数
1、返回字符串 s 的第一个字符的 ASCII码
select ascii('ab');
-- 返回a的ASCII码值:65
2、length / char_length(s) / character_length(s)返回字符串 s 的字符数
select length('1234');
-- 返回4
3、concat(s1,s2…sn) / concat_ws(s,s1…sn)字符串 s1,s2 等多个字符串合并为一个字符串,后者可以添加分隔符s
select concat('hel','llo');
-- 返回hello
select concat_ws("-", "sql", "tutorial", "is", "fun!")
-- sql-tutorial-is-fun!
4、find_in_set(s1,s2)返回在字符串s2中与s1匹配的字符串的位置
select find_in_set("c", "a,b,c,d,e");
-- 返回3
5、format(x,n)函数可以将数字 x 进行格式化 “#,###.##”, 将 x 保留到小数点后 n 位,最后一位四舍五入
select format(250500.5634, 2);
-- 返回250,500.56
6、insert(s1,x,len,s2)字符串 s2 替换 s1 的 x 位置开始长度为 len 的字符串
select insert("google.com", 1, 6, "runnob");
-- 返回runoob.com
7、locate(s1,s)从字符串 s 中获取 s1 的开始位置
select locate('st','myteststring');
-- 返回5
8、lcase(s) / lower(s)将字符串 s 的所有字母变成小写字母
select lower('runoob');
-- 返回runoob
9、ucase(s) / upper(s)将字符串 s 的所有字母变成大写字母
select ucase('runoob');
-- 返回runoob
10、trim(s)去掉字符串 s 开始和结尾处的空格
select trim(' runoob ');
-- 返回runoob
11、ltrim(s)去掉字符串 s 开始处的空格
select ltrim(' runoob ');
-- 返回 ’runoob ‘
12、rtrim(s)去掉字符串 s 结尾处的空格
select rtrim(' runoob ');
-- 返回 ’ runoob‘
13、substr(s, start, length)从字符串 s 的 start 位置截取长度为 length 的子字符串
select substr("runoob", 2, 3) as extractstring;
-- 从字符串 runoob 中的第 2 个位置截取 3个 字符,返回uno
14、substr / substring(s, start, length)从字符串 s 的 start 位置截取长度为 length 的子字符串
select substr / substring("runoob", 2, 3);
-- 从字符串 runoob 中的第 2 个位置截取 3个 字符,返回uno
15、position(s1 in s)从字符串 s 中获取 s1 的开始位置
select position('b' in 'abc');
-- 返回2
16、repeat(s,n)将字符串 s 重复 n 次
select repeat('runoob',3);
-- 返回runoobrunoobrunoob
17、reverse(s)将字符串s的顺序反过来
select reverse('abc');
-- 返回cba
18、strcmp(s1,s2)比较字符串 s1 和 s2,如果 s1 与 s2 相等返回 0 ,如果 s1>s2 返回 1,如果 s1<s2 返回 -1
select strcmp("runoob", "runoob");
-- 返回0
19、substring_index(s1,s2,n)返回一个字符串s1在出现指定数量n的分隔符s2之前的子字符串
select substring_index("www.w3schools.com", ".", 2);
-- www.w3schools
# 也可以反向截取
select substring_index("www.w3schools.com", ".", -1);
-- com
(三)日期函数
1、curdate() / current_date()返回当前日期
select curdate();
select current_date();
-- 返回2019-02-19
2、current_time() / curtime()返回当前时间
select current_time();
-- 返回11:40:45
3、current_timestamp()返回当前日期和时间
select current_timestamp();
-- 返回2019-02-19 11:41:32
4、adddate(d,n)计算起始日期 d 加上 n 天的日期
select adddate("2017-06-15", interval 10 day);
-- 返回2017-06-25
5、addtime(t,n)时间 t 加上 n 秒的时间
select addtime('2011-11-11 11:11:11', 5);
-- 返回2011-11-11 11:11:16
6、date()从日期或日期时间表达式中提取日期值
select date("2017-06-15 11:11:16");
-- 返回2017-06-15
7、day(d)返回日期值 d 的日期部分
select day("2017-06-15");
-- 返回15
8、datediff(d1,d2)计算日期 d1->d2 之间相隔的天数
select datediff('2001-01-01','2001-02-02');
-- 返回-32
9、date_format按表达式 f的要求显示日期 d
select date_format('2011.11.11 11:11:11','%y-%m-%d %r');
-- 返回2011-11-11 11:11:11 am
10、dayname(d)返回日期 d 是星期几,如 monday,tuesday
select dayname('2011-11-11 11:11:11');
-- 返回friday
11、dayofmonth(d)计算日期 d 是本月的第几天
select dayofmonth('2011-11-11 11:11:11');
-- 返回11
12、dayofweek(d)日期 d 今天是星期几,1 星期日,2 星期一,以此类推
select dayofweek('2011-11-11 11:11:11');
-- 返回6
13、dayofyear(d)计算日期 d 是本年的第几天
select dayofyear('2011-11-11 11:11:11');
-- 返回315
14、extract(type from d)从日期 d 中获取指定的值,type 指定返回的值
type可取值为:
microsecond
second
minute
hour
day
week
month
quarter
year
second_microsecond
minute_microsecond
minute_second
hour_microsecond
hour_second
hour_minute
day_microsecond
day_second
day_minute
day_hour
year_month
select extract(minute from '2011-12-13 14:15:16');
-- 返回15
15、dayofweek(d)日期 d 今天是星期几,1 星期日,2 星期一,以此类推
select dayofweek('2011-11-11 11:11:11');
-- 返回6
16、unix_timestamp()得到时间戳
select unix_timestamp('2019-2-19');
select unix_timestamp(expression);
-- 返回1550505600
17、from_unixtime()时间戳转日期
select from_unixtime(1550505600);
-- 返回2019-02-19 00:00:00
select from_unixtime(1550505600, '%y-%m-%d');
-- 返回2019-02-19
(四)sql高级函数
1、if(expr,v1,v2)如果表达式 expr 成立,返回结果 v1;否则,返回结果 v2
select if(1>0,'yes','no');
-- 返回yes
2、conv(x,f1,f2)返回 f1 进制数变成 f2 进制数
select conv(13,10,2);
-- 返回1101
3、current_user() / session_user() / system_user() / user()返回当前用户
select current_user();
-- root@localhost
4、database()返回当前数据库名
select database();
-- 返回当前使用的数据库名
5、version()返回数据库的版本号
select version();
-- 8.0.12
6、case when 语句多条件判断
用法一:简单case函数
case 条件参数名称
when 参数值1 then '显示值1'
when 参数值2 then '显示值2'
...
else '显示其他值' end
用法二:case搜索函数
case
when 条件参数名称 = '参数值1' then '显示值1'
when 条件参数名称 = '参数值2' then '显示值2'
...
else '显示其他值' end
7、lead / lag(expr,N,default)向下/上 N行取值,如果超出expr的最大/最小行数则返回default
准备如下数据
drop table if exists exam_record;
create table exam_record
(
id int primary key auto_increment comment '自增id',
uid int not null comment '用户id',
exam_id int not null comment '试卷id',
start_time datetime not null comment '开始时间',
submit_time datetime comment '提交时间',
score tinyint comment '得分'
) character set utf8
collate utf8_general_ci;
insert into
exam_record(uid, exam_id, start_time, submit_time, score)
values
(1001, 9001, '2020-01-01 09:01:01', '2020-01-01 09:21:59', 90),
(1002, 9001, '2020-01-20 10:01:01', '2020-01-20 10:10:01', 89),
(1002, 9001, '2020-02-01 12:11:01', '2020-02-01 12:31:01', 83),
(1003, 9001, '2020-03-01 19:01:01', '2020-03-01 19:30:01', 75),
(1004, 9001, '2020-03-01 12:01:01', '2020-03-01 12:11:01', 60),
(1003, 9001, '2020-03-01 12:01:01', '2020-03-01 12:41:01', 90),
(1002, 9001, '2020-05-02 19:01:01', '2020-05-02 19:32:00', 90),
(1001, 9002, '2020-01-02 19:01:01', '2020-01-02 19:59:01', 69),
(1004, 9002, '2020-02-02 12:01:01', '2020-02-02 12:20:01', 99),
(1003, 9002, '2020-02-02 12:01:01', '2020-02-02 12:31:01', 68),
(1001, 9002, '2020-02-02 12:01:01', '2020-02-02 12:43:01', 81),
(1001, 9002, '2020-03-02 12:11:01', null, null);
# 向下N行取值
select id, lead(id, 2, 0) over () id_lead
from exam_record
-- id,id_lead
-- 1,3
-- 2,4
-- 3,5
-- 4,6
-- 5,7
-- 6,8
-- 7,9
-- 8,10
-- 9,11
-- 10,12
-- 11,0
-- 12,0
# 向上N行取值
select id, lag(id, 2, 0) over ()
from exam_record
-- id,id_lag
-- 1,0
-- 2,0
-- 3,1
-- 4,2
-- 5,3
-- 6,4
-- 7,5
-- 8,6
-- 9,7
-- 10,8
-- 11,9
-- 12,10