当前位置: 代码网 > it编程>数据库>Mysql > mysql中find_in_set()函数用法及自定义增强函数

mysql中find_in_set()函数用法及自定义增强函数

2024年08月13日 Mysql 我要评论
一、find_in_set()我们知道mysql提供了一个好用的函数find_in_set(str,strlist),该函数的作用是查询字段(strlist)中是否包含(str)的结果,返回结果为nu

一、find_in_set()

我们知道mysql提供了一个好用的函数

find_in_set(str,strlist),

该函数的作用是查询字段(strlist)中是否包含(str)的结果,
返回结果为null或记录 。

str 要查询的字符串
strlist 需查询的字段,参数以”,”分隔,例如如 '1,2,3'。

下面有一组示例

select find_in_set('1', '1,2,3');
// 结果:1
select find_in_set('3', '1,2,3');
// 结果:3
select find_in_set('4', '1,2,3');
// 结果:0
// 后一个包含前一个返回大于0的元素所在位置,不包含前一个则返回0

我们一般在查询的where条件使用 find_in_set(str,strlist)>0,则说明strlist包含str

但是这个函数的第一个参数只能判断是单个字符串,如果我有以下需求

1. 判断字符串 '1,3' 中的元素是否有任意一个元素存在字符串 '1,3,4,5,7',意思就是1或者3只要有任意一个存在字符串 '1,3,4,5,7' 中就算匹配成功。

2. 再比如判断字符串 '1,3,5' 中的所有元素是否都存在于字符串 '1,3,4,5,7' 中,即1,3,5每个元素都要在 字符串 '1,3,4,5,7'中能找到才算匹配成功。

针对需求1,提供了一个叫 find_part_in_set  的函数

针对需求2,提供了一个叫 find_all_part_in_set 的函数

二、find_part_in_set

create function `find_part_in_set`(str1 text, str2 text)
    returns text
begin
    #传入两个逗号分割的字符串,判断第二个字符串是否包含第一个字符串split之后的单个
    declare currentindex int;#当前下标
    declare currentstr text;
    declare result int;
    set result = 0;
    set currentindex = 0;
    set currentstr = '';
    if str1 is not null and str1 != '' then
        set currentindex = locate(',', str1);
        while currentindex > 0
            do
                set currentstr = substring(str1, 1, currentindex - 1);
                if find_in_set(currentstr, str2)>0 then
                    set result = 1;
                end if;
                set str1 = substring(str1, currentindex + 1);
                set currentindex = locate(',', str1);
            end while;
        #只传一个 和 最后无逗号的情况
        if length(str1) > 0 then
            if find_in_set(str1, str2)>0 then
                set result = 1;
            end if;
        end if;
    end if;
    return result;
end;

实际调用判断find_part_in_set(str1 ,str2)>0即可,例如find_part_in_set('1,3' , '1,3,4,5')>0

三、find_all_part_in_set

create function `find_all_part_in_set`(str1 text, str2 text)
    returns text
begin
    #传入两个逗号分割的字符串,判断第二个字符串是否全部包含第一个字符串split之后的单个
    declare currentindex int;#当前下标
    declare currentstr text;
    declare result int;
    declare totalcount int;
    declare truecount int;
    set result = 0;
    set currentindex = 0;
    set currentstr = '';
    set totalcount = 0;
    set truecount = 0;
    if str1 is not null and str1 != '' then
        set currentindex = locate(',', str1);
        while currentindex > 0
            do
                set totalcount = totalcount + 1;
                set currentstr = substring(str1, 1, currentindex - 1);
                if find_in_set(currentstr, str2)>0 then
                    set truecount = truecount + 1;
                end if;
                set str1 = substring(str1, currentindex + 1);
                set currentindex = locate(',', str1);
            end while;
        #只传一个 和 最后无逗号的情况
        if length(str1) > 0 then
            set totalcount = totalcount + 1;
            if find_in_set(str1, str2)>0 then
                set truecount = truecount + 1;
            end if;
        end if;
    end if;
    if totalcount > 0 and truecount = totalcount then
        set result = 1;
    end if;
    return result;
end;

实际调用判断find_all_part_in_set(str1 ,str2)>0即可,例如find_part_in_set('1,3,5' , '1,3,4,5,7')>0 

到此这篇关于mysql中find_in_set()函数用法及自定义增强函数的文章就介绍到这了,更多相关mysql find_in_set()内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com