• MYSQL FIND_IN_SET函数


    find_in_set 函数的语法:


    FIND_IN_SET(str,strList)

    • str 要查询的字符串
    • strList 字段名,参数以“,”分隔,如(1,2,6,8)
    • 查询字段(strList)中包含的结果,返回结果null或记录。

    假如字符串str在由N个子链组成的字符串列表strlist 中,则返回值的范围在 1 到 N 之间。 一个字符串列表就是一个由一些被 ‘,’ 符号分开的子链组成的字符串。如果第一个参数是一个常数字符串,而第二个是type SET列,则FIND_IN_SET() 函数被优化,使用比特计算。 如果str不在strlist 或strlist 为空字符串,则返回值为 0 。如任意一个参数为NULL,则返回值为 NULL。这个函数在第一个参数包含一个逗号(‘,’)时将无法正常运行。


    例子一 基础用法

    1
    select find_in_set('1','1,2,3,4,5,6');
    2
    select find_in_set('2','1,2,3,4,5,6');
    0
    select find_in_set('7','1,2,3,4,5,6');
    0
    select find_in_set('2','1,21,3,4,5,6');
    0
    select find_in_set('1','');
    null
    select find_in_set(null,'1,2,3,4,5,6');
    null
    select find_in_set('1',null);
    

    例子二 find_in_set()和in的区别

    创建表,并添加数据

    CREATE TABLE `tb_test` (
      `id` int(8) NOT NULL auto_increment,
      `name` varchar(255) NOT NULL,
      `list` varchar(255) NOT NULL,
      PRIMARY KEY  (`id`)
    );
    INSERT INTO `tb_test` VALUES (1, 'name', 'daodao,xiaohu,xiaoqin');
    INSERT INTO `tb_test` VALUES (2, 'name2', 'xiaohu,daodao,xiaoqin');
    INSERT INTO `tb_test` VALUES (3, 'name3', 'xiaoqin,daodao,xiaohu');
    

    查询一

    SELECT * from tb_test where "daodao" in (list);
    

    结果

    img

    捕获.PNG

    解析:
    实际上这样是不行的,这样只有当list字段的值等于'daodao'时(和IN前面的字符串完全匹配),查询才有效,否则都得不到结果,即使'daodao'真的在list中。

    SELECT * from tb_test where "daodao,xiaohu,xiaoqin" in (list);
    

    执行上面这条语句,才能返回结果。

    img

    捕获.PNG

    查询二

    SELECT * from tb_test WHERE 'daodao' IN ('libk', 'zyfon', 'daodao')
    

    结果

    img

    捕获.PNG

    解析:
    这两条到底有什么区别呢?为什么第一条不能取得正确的结果,而第二条却能取得结果。原因其实是(一)中 (list) list是变量, 而(二)中 ('libk', 'zyfon', 'daodao')是常量

    查询三

    SELECT * from tb_test where FIND_IN_SET("daodao",list)
    

    结果

    img

    捕获.PNG

    解析:
    find_in_set 函数 这里的“list” 是 变量

    例子三 find_in_set()和like的区别

    在mysql中,有时我们在做数据库查询时,需要得到某字段中包含某个值的记录,但是它也不是用like能解决的,使用like可能查到我们不想要的记录,它比like更精准,这时候mysql的FIND_IN_SET函数就派上用场了

    创建表,并插入数据

    CREATE TABLE users(
        id int(6) NOT NULL AUTO_INCREMENT,
        name VARCHAR(20) NOT NULL,
        limits VARCHAR(50) NOT NULL, -- 权限
        PRIMARY KEY (id)
    );
    
    INSERT INTO users(name, limits) VALUES('小张','1,2,12'); 
    INSERT INTO users(name, limits) VALUES('小王','11,22,32');
    

    其中limits表示用户所拥有的权限(以逗号分隔),现在想查询拥有权限编号为2的用户,如果用like关键字的话,则查询结果如下:

    SELECT * FROM users WHERE limits LIKE '%2%';
    

    img

    捕获.PNG

    很显然,结果不符合预期,下面用find_in_set 函数 来 解决。

    SELECT * from users where FIND_IN_SET('2',limits)
    

    img

    捕获.PNG

    解析:
    mysql字符串函数 find_in_set(str1,str2)函数是返回str2中str1所在的位置索引,str2必须以","分割开

    总结:like是广泛的模糊匹配,字符串中没有分隔符,Find_IN_SET 是精确匹配,字段值以英文”,”分隔,Find_IN_SET查询的结果要小于like查询的结果。

    例子四

    ···
    select t.id from
    (
    select 1 as id, 'nick' as name
    union all
    select 2 as id, 'viki' as name
    union all
    select 3 as id, 'robin' as name
    union all
    select 4 as id, 'teng' as name
    union all
    select 5 as id, 'mike' as name
    union all
    select 6 as id, 'will' as name
    ) t
    where find_in_set(t.id, '2,3,4') > 0;
    ···
    结果

    img

    捕获.PNG

    参考文献:https://blog.csdn.net/chenpeng19910926/article/details/51790034
    [https://blog.csdn.net/c_base_jin/article/details/74358235](


    本文转自https://www.jianshu.com/p/b2c1ba0ba34f

  • 相关阅读:
    mysql 无法连接提示 Authentication plugin 'caching_sha2_password' cannot be loaded
    探究分析:快速对大量的数据转换为数组
    SQL Server like 字段
    InfluxDB从原理到实战
    Python学习日记(四十) Mysql数据库篇 八
    MySQL数据库基本操作
    ES入门宝典(详细截图版)
    NameNode && Secondary NameNode工作机制
    MySQL 两张表关联更新(用一个表的数据更新另一个表的数据)
    mysql单个表拆分成多个表
  • 原文地址:https://www.cnblogs.com/snake107/p/12053086.html
Copyright © 2020-2023  润新知