MySQL常用函数大全及案例
字符串转换函数
convert(expr using transcoding_name): 将值转换为指定的数据类型或字符集。
select charset('ABC'), charset(convert('ABC' using gbk));
select convert('2022-05-25', date), now()
, convert('2022-05-25', datetime)
, convert('14:05:37', time), convert(now(), time);
select convert(150, char) as str1, concat('Hello World', 437) as str2
, convert('5.0', signed) as str3, convert(5 - 10, signed) as str4
, convert('5.0', unsigned) as str5, convert(5 - 10, unsigned) as str6;
select convert('9.0', decimal) as str1, convert('9.5', decimal(10, 2)) as str2
, convert('123456789.1234', decimal(10, 2)) as str3
, convert('220.2321234', decimal(10, 3)) as str4
, convert(220.2321234, decimal(10, 4)) as str5;
一. 字符函数
1. substring(str, pos, [len]): 返回从指定位置pos开始的字串,len表示返回子串的长度,pos为0表示返回空字符串。位置参数pos可以为负数,此时返回的子串从字符串右侧第pos个字符开始。
select substring('mysql字符串函数', 6) as str1
,substring('mysql字符串函数', 6, 3) as str2
, substring('mysql字符串函数', -2) as str3
, substring('mysql字符串函数', -5, 3) as str4
, substring('mysql字符串函数', 0) as str5;
另外,substr() 和mid() 函数都是substring函数的同义词,也支持以上几种形式。
left(str, len)函数返回字符串str 左侧的len个字符,right 函数返回字符串str 右侧的len个字符。
select left('mysql字符串函数', 5) as str1, right('mysql字符串函数', 5) as str2;
2. length(str): 返回字符串的字节长度(在utf8_mb4编码中,一个汉字占用三个字节);
char_length(str):返回字符串的字符长度(即字符个数);
bit_length(str):返回字符串的比特长度(求比特数量,一个字节占8个比特);
select length('mysql字符串函数') as ‘求字节长度’
, char_length(‘mysql字符串函数’) as ‘求字符长度’
, bit_length(‘mysql字符串函数’) as ‘求二进制长度’;
3. lpad(str, len, padstr) /rpad(): 字符串str的左侧使用padstr进行填充, 直到长度为len。当字符串str的长度大于len时,相当于从右侧截断字符串。
select lpad(123, 6, '0') as str1, rpad(123, 2, '0') as str2
, rpad(123, 6, '0') as str3, rpad(123, 2, '0') as str4;
4. upper()/lower(): 将字符串转换为大/小写形式,mysql大小写转换函数不支持二进制字符串(binary、varbinary、blob),可以将其转换为非二进制的字符串后在进行处理。
select upper('Hello World') as str1, lower('Hello World') as str2
, lower(binary 'MySQL字符串函数') as str3
, lower(convert(binary 'MySQL字符串函数' using utf8mb4)) as str4;
5. replace(str, from_str, to_str):将字符串str中所有的from_str替换为to_str,返回替换后的字符串。
select replace('mysql字符串函数', '字符串', '日期') as str1
, replace('mysql字符换函数', '函数', '日期') as str2;
6. reverse(str):用于将字符串str中的字符顺序进行反转。
select reverse(12345) as ‘基本显示’, reverse('上海自来水来自海上')=‘上海自来水来自海上’;
7. trim( [remstr from] str): 函数用于返回删除字符串str 两侧所有remstr字符串之后的字符, remstr默认为空格。
trim([{both | leading | trailing} [] from] str): 用于返回删除字符串str两侧/左侧/右侧所有remstr字符串之后的子串,默认删除两侧字符串,remstr默认为空格。
select trim(' mysql字符串函数 ') as str1
, trim('-' from '----mysql字符串函数----') as str2
, trim(leading '-' from '----mysql字符串函数----') as str3
, trim(trailing '-' from '----mysql字符串函数----') as str4;
8. instr(str, substr): 返回子串substr在字符串str中第一次出现的索引位置,没有找到子串时返回0。(空字符串被认为是在任何字符串的开头)
select instr('mysql字符串函数', '字符串') as str1
, instr('mysql字符串函数', '日期') as str2
, instr('mysql字符串函数', 'null') as str3
, instr('mysql字符串函数', '') as str4;
扩展,locate(substr, str, pos) 用于返回子串 substr 在字符串 str 中从位置 pos 开始第一次出现的索引位置; field(str, str1, str2, ...)用于返回字符串 str 在后续字符串列表中出现的位置,没有找到时返回 0; find_in_set(str, strlist) 用于返回字符串 str 在列表字符串 strlist 中出现的位置,strlist 由 n 个子串使用逗号分隔组成。
select locate('5', 'MySQL Server', 5) as str1
, field('李四', '张三', '李四', '王五', '赵六') as str2
, find_in_set('李四', '张三,李四,王五,赵六') as str3;
9. concat(): 返回结果为连接参数产生的字符串
select concat('a', 'b', 'c');
10. concat_ws(separator, str1, str2, ...):第一个参数是其他参数的分隔符。分隔符的位置放在要连接的两个字符串之间。分隔符可以是一个字符串,也可以是其他参数。如果分隔符为null,则结果为null,函数会忽略任何分隔符参数后的null值。
select concat_ws(' ', 'hello', 'world');
二. 时间函数
1. 时间函数
| 函数 |
描述 |
|---|---|
| now() | 返回当前的日期和时间 |
| curdate() | 返回当前日期 |
| curtime() | 返回当前时间 |
| date() | 提取日期或日期、时间表达式的日期部分 |
| extract() | 返回日期、时间按的单独部分 |
| date_add() | 给日期添加指定的时间间隔 |
| date_sub() | 从日期减去指定的时间间隔 |
| datediff() | 返回两个日期之间的天数 |
| date_formate() | 用不同的格式显示日期时间 |
| month(d) | 返回日期d中的月份值,范围是1~12 |
| sysdate() | 返回函数执行时的日期和时间 |
| dayname() | 返回工作日名称 |
2. 日期/时间格式
| date 格式 | YYYY-MM-DD |
| datetime 格式 | YYYY-MM-DD HH: MM: SS |
| timestamp 格式 | YYYY-MM-DD HH: MM: SS |
| year 格式 | YYYY 或 YY |
三.数字函数
1. 加减乘除类
| 函数 | 作用 |
|---|---|
| abs() | 求绝对值 |
| sqrt() | 求平方根 |
| mod(x, y) | x除以y得到的余数 |
| pow(x, y) | 计算x的y次幂 |
| power(x, y) | 计算x的y次方 |
| truncate(x, y) | 将x阶段为小数点后y位保留 |
2.聚合类
| 函数 | 作用 |
|---|---|
| avg(x) | 求平均值 |
| count(expr) | 求表达式行数 |
| greatest(expr1, expr2, expr3, ...) | 返回一组表达式中的最大值 |
| least(expr1, expr2, expr3, ...) | 返回表达式中的最小值 |
| sum() | 计算一组数值的总和 |
| max()/min() | 求最大值和最小值 |
3. 排名类
| 函数 | 作用 |
|---|---|
| row_number() | 为每行分配唯一的连续序号 |
| rank() | 为行分配排名,并列时跳过后续序号 |
| dense_rank() | 为行分配排名,并列是不跳过后续序号 |
| ntile() | 将数据分为n个大致相等的数 |
4. 其他类
| 函数 | 作用 |
|---|---|
| ceil(x) | 向上取整 |
| floor(): | 向下取整 |
| randians(x) | 将角度值转换为弧度值 |
| round(x[, y]) | 把参数x四舍五入到指定y的小数位数 |
| sign(x) | x为正,返回1;x为负,返回-1;x为0,返回0 |
5. 偏移类
| 函数 | 作用 |
|---|---|
| lead() | 获取当前行之前的值 |
| lag() | 获取当前行之后的值 |
| first_value() | 获取窗口框架内第一个值 |
| last_value() | 获取窗口框架内最后一个值 |
SQL案例暂时略过
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐
所有评论(0)