mysql中的字符串分割函数
mysql本身没有内置的split()
函数,但可以通过其他方式实现字符串分割功能。以下是几种常见的方法:
1. substring_index函数
substring_index()
是mysql中最常用的字符串分割函数,它可以根据指定的分隔符从字符串中提取子串,语法如下:
substring_index(str, delim, count)
含义
:返回字符串 str 中按分隔符 delim 分割后的第 count 个子串。str
: 要分割的字符串delim
: 分隔符(可以是单个字符或多个字符)count
:- 正数:返回从左边开始第count个分隔符之前的所有内容
- 负数:返回从右边开始第count个分隔符之后的所有内容
1、基本用法
-- 获取第一个逗号前的内容 select substring_index('apple,banana,orange', ',', 1); -- 结果: 'apple' -- 获取最后一个逗号后的内容 select substring_index('apple,banana,orange', ',', -1); -- 结果: 'orange' -- 获取前两个元素 select substring_index('apple,banana,orange', ',', 1) as item1, substring_index(substring_index('apple,banana,orange', ',', 2), ',', -1) as item2; -- 结果: item1='apple', item2='banana'
2. 处理多字符分隔符
-- 使用多字符作为分隔符 select substring_index('apple||banana||orange', '||', 2); -- 结果: 'apple||banana' select substring_index('apple||banana||orange', '||', -1); -- 结果: 'orange'
- 找到第一个 || 在 apple||banana||orange 的 apple 之后,此时已找到1次分隔符
- 找到第二个 || 在 banana 之后,此时已找到2次分隔符(达到count值)
- 函数返回从开头到第二个 || 之前的所有内容:‘apple||banana’
3. 边界情况处理
-- 分隔符不存在时返回原字符串 select substring_index('apple_banana_orange', ',', 1); -- 结果: 'apple_banana_orange' -- count超过实际分隔数时返回整个字符串 select substring_index('apple,banana', ',', 5); -- 结果: 'apple,banana' -- 空字符串处理 select substring_index('', ',', 1); -- 结果: ''
2. 使用正则表达式:regexp_substr
mysql 8.0及以上版本支持正则表达式函数:
-- 使用regexp_substr提取匹配的子串 select regexp_substr('apple,banana,orange', '[^,]+', 1, 1) as item1, regexp_substr('apple,banana,orange', '[^,]+', 1, 2) as item2; -- 结果: item1='apple', item2='banana'
3. 使用存储过程实现完整分割
如果需要将字符串完全分割成多行,可以创建存储过程:
delimiter // create procedure split_string(in input_string varchar(1000), in delimiter_char varchar(1)) begin declare temp_string varchar(1000); declare i int default 1; declare item varchar(1000); set temp_string = input_string; while length(temp_string) > 0 do set item = substring_index(temp_string, delimiter_char, 1); select item as split_result; set temp_string = substring(temp_string, length(item) + 2); if length(temp_string) = 0 then leave; end if; set i = i + 1; end while; end // delimiter ; -- 调用存储过程 call split_string('apple,banana,orange', ',');
4. 使用json函数(mysql 5.7+)
mysql 5.7及以上版本可以使用json函数处理字符串分割:
-- 将逗号分隔的字符串转为json数组 select json_unquote(json_extract(concat('["', replace('apple,banana,orange', ',', '","'), '"]'), '$[0]')) as item1, json_unquote(json_extract(concat('["', replace('apple,banana,orange', ',', '","'), '"]'), '$[1]')) as item2; -- 结果: item1='apple', item2='banana'
实际应用示例
假设有一个表格包含逗号分隔的标签字段:
create table products ( id int, name varchar(100), tags varchar(255) ); insert into products values (1, 'laptop', 'electronics,computer,premium'), (2, 'phone', 'electronics,mobile'), (3, 'desk', 'furniture,office'); -- 查询所有电子类产品 select * from products where find_in_set('electronics', tags) > 0;
注意:对于复杂的字符串分割需求,建议在应用层处理(如python、java等),或者在数据库设计时就避免使用分隔符存储多个值(遵循第一范式)。
练习题
select substring_index (profile, ',', -1) as gender, count(*) as number from user_submit group by gender
select device_id, substring_index (blog_url, "/", -1) as user_name from user_submit
select substring_index (substring_index (profile, ",", 3), ",", -1) as age, count(*) as number from user_submit group by age
select # substring_index (order_id, '_', -1) as product_id, regexp_substr(order_id,'p[0-9]+$') as product_id, count(*) as cnt from order_log group by product_id order by cnt desc,product_id asc;
- ‘p[0-9]+$’: 正则表达式模式
- p: 匹配字母 “p”
- [0-9]+: 匹配一个或多个数字(0-9)
- $: 匹配字符串的结尾
到此这篇关于mysql中的字符串分割函数的文章就介绍到这了,更多相关mysql字符串分割函数内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论