文章目录
- 一、数据类型函数
-
- 1、数学函数
-
- 1)向上取整
- 2)向下取整
- 3)四舍五入
- 4)非四舍五入
- 5)随机数
- 2、字符串函数
-
- 1)字符串拼接
- 2)获取字符串的长度
- 3)获取子串在字符串出现的位置
- 4)插入、替换字符串
- 5)替换
- 6)转大小写
- 7)去除字符串两端内容
- 8)截取字符串
- 9)重复
- 10)反转
- 11)比较
- 3、日期函数
-
- 1)获取当前日期、时间
- 2)提取
- 3)运算
- 4)格式化
- 5)时间戳转换
- 6)比较
- 4、json函数
-
- 1)构建jsonObject对象
- 2)构建jsonArray对象
- 3)是否包含某个值
- 4)替换
- 5)获取字段类型
- 6)加引号
- 7)去除json字符串的引号,转成字符串
- 8)提取json值
- 9)获取所有key
- 5、其它
-
- 二、聚合函数
-
- 1、平均值
- 2、最大值
- 3、最小值
- 4、求和
- 5、统计数量
- 三、其它
-
- 1、if
- 2、ifnull
- 3、分组汇总统计with rollup
- 4、自定义排序函数order by field
- 5、UUID
一、数据类型函数
1、数学函数
1)向上取整
ceil(12.12)
2)向下取整
floor(12.12)
3)四舍五入
round(num)
m:保留几位小数
round(num,m)
4)非四舍五入
truncate(num,m)
truncate(12.8,0)
5)随机数
rand()
N:种子值
rand(N)
2、字符串函数
1)字符串拼接
concat('aa','bb')
concat_ws(separator,str1,str2 . . .)
concat_ws(',','aa','bb')
2)获取字符串的长度
char_length('abc')
char_length('轻松工作')
length('abc')
length('轻松工作')
3)获取子串在字符串出现的位置
instr('abcdef','c')
locate('c','abcde')
find_in_set('3','3,13,33,36,39')
find_in_set('3','13,33,36,39')
4)插入、替换字符串
insert(str,start,length,newstr)
insert('abcdef',2,2,'m')
5)替换
replace('abcdefg','c','x')
6)转大小写
upper(str)
lower(str)
7)去除字符串两端内容
trim(' ab ')
trim(子串 from str)
trim('e' from 'essse')
8)截取字符串
left(str,n)
right(str,n)
substring(str,start,length)
substring_index(str,delim,count)
substring_index('www.wikibt.com','.',1)
substring_index('www.wikibt.com','.',-2)
substring_index(substring_index('www.wikibt.com','.',2),'.',-1)
9)重复
repeat(str,n)
repeat('ab',2)
10)反转
reverse(str)
reverse('abc')
11)比较
- 字符串在进行比较时和java的字符串比较规则一样,‘121’>‘12000’,所以一般使用类型转换函数
3、日期函数
1)获取当前日期、时间
now()
sysdate()
curdate()
curtime()
2)提取
date(now())
time(now())
extract(year from now())
extract(month from now())
extract(day from now())
extract(hour from now())
extract(minute from now())
extract(second from now())
3)运算
1、加
DATE_ADD(date,INTERVAL expr unit)
2、减
SUBDATE(date,INTERVAL expr unit)
DATE_SUB(date,INTERVAL expr unit)
4)格式化
format |
说明 |
%Y |
四位年 |
%y |
两位年 |
%m |
两位月 |
%c |
一位月 |
%d |
日 |
%H |
24小时 |
%h |
12小时 |
%i |
分 |
%s |
秒 |
- 日期时间转字符串
date_format(date,format)
date_format(now(),'%Y年%m月%d日 %H时%i分%s秒')
date_format(date(now()),'%Y-%m-%d 00:00:00')
- 字符串转日期时间
str_to_date(str,format)
str_to_date('16.11.2018 15.00.00','%d.%m.%Y %H.%i.%s')
5)时间戳转换
- 日期转换为时间戳
UNIX_TIMESTAMP(now())
- 时间戳转换为指定格式的日期
FROM_UNIXTIME(unix_timestamp)
FROM_UNIXTIME(unix_timestamp,format)
6)比较
4、json函数
1)构建jsonObject对象
json_objectagg(key,value)
json_object(key,val. . .)
json_object('id', 87, 'name', 'carrot')
2)构建jsonArray对象
json_arrayagg(val)
json_array(val. . .)
json_array(1,"abc",null)
3)是否包含某个值
json_contains(target, candidate[, path])
json_contains(json_array('1','2'),json_arrayagg('3'))
json_contains(字段, ' "www" ', '$.url')
json_contains(字段, '{"url": "www.cctv"}')
4)替换
json_replace(json_doc, path, val[, path, val] ...)
json_replace(content, '$.name', "tom")
5)获取字段类型
json_type(TARGET / TARGET->'$.params')
6)加引号
json_quote
7)去除json字符串的引号,转成字符串
json_unquote(json_val)
json_unquote(json_extract(tt, '$.name'))
8)提取json值
json_extract(json_doc, path[, path] ...)
JSON_EXTRACT(json_value, '$.name')
9)获取所有key
json_keys(json_doc[, path])
JSON_KEYS(json_object('name','kimi','age',18))
5、其它
1)类型转换
type |
说明 |
CHAR |
定长字符串 |
DATE |
日期 |
TIME |
时间 |
DATETIME |
日期时间 |
DECIMAL |
浮点数 |
SIGNED |
整数 |
UNSIGNED |
无符号整数 |
JSON |
json |
cast(expr as type)
cast(123.4 as int)
cast('2021-11-11' as date)
cast('{"name":"kimi"}' as JSON)
convert(expr,type)
convert('123.3',SIGNED)
二、聚合函数
1、平均值
avg(column)
select
avg(age)
from t_user
2、最大值
max(column)
3、最小值
min(column)
4、求和
sum(colnum)
sum(colnum+column+...)
sum(语文+数学)
sum(ifnull(语文,0)+ifnull(数学,0))
ifnul(sum(colnum),0)
5、统计数量
count(column)
count(*)
count(id)
三、其它
1、if
drop table if exists t_user;
select if(3>2,2,3);
select if(score is null,'',if(score=0,0,5));
order by if(status!='3' or status is null,end_time,update_time) desc;
update table set status=if(
2、ifnull
age = ifnull(x,y)
update t set sal=ifnull(sal,25.000);
3、分组汇总统计with rollup
select
ifnull(name,'合计') nn,
sum(sal) sal
from emp
group by name with rollup
4、自定义排序函数order by field
5、UUID
select UUID();
select UUID_SHORT();