zoukankan      html  css  js  c++  java
  • mysql find_in_set 函数

    > 简单的例子

    这个函数的功能是,在第二个参数中寻找第一个参数,并返回第一个参数所在的位置,不存在则返回0。其中,第二个参数是以“,”分隔的字符串。

      -- 1

    1. select find_in_set('1', '1,2,3,4,5,6');

    2.  
    3. -- 2

    4. select find_in_set('2', '1,2,3,4,5,6');

    5.  
    6. -- 0

    7. select find_in_set('7', '1,2,3,4,5,6');

    8.  
    9. -- 0

    10. select find_in_set('2', '1,21,3,4,5,6');

    11.  
    12. -- 0

    13. select find_in_set('2', '');

    14.  
    15. -- 0

    16. select find_in_set('2', '1,2 ,3,4,5,6');

    17.  
    18. -- NULL

    19. select find_in_set(NULL, '1,2,3,4,5,6');

    20.  
    21. -- NULL

    22. select find_in_set(1, NULL);

    23.  
    24. -- 0

    25. select find_in_set('2,3', '1,2,3,4,5,6');

    > 可替换分隔字符串的部分使用功能

    有时候,我们获取了“2,3,4”字符串,我们想把它根据“,”分隔成5行,每行分别是2、3、4。

    这时候,我们自然想到类似Java的String的split的函数,可是MySQL貌似并无类似的函数。(我没有找到,如果有的话请通知我哦)

    而实际上,“2,3,4”是有一定业务意义的,或者说出自某一张表的,那么可以使用find_in_set把“2,3,4”从对应的表中查找出来。一般来说,该键有索引,从大量的数据找出少量的数据,效率是很高的。

    下面是简单的例子:

    select t.id from

    1. (

    2. select 1 as id, 'nick' as name

    3. union all

    4. select 2 as id, 'viki' as name

    5. union all

    6. select 3 as id, 'robin' as name

    7. union all

    8. select 4 as id, 'teng' as name

    9. union all

    10. select 5 as id, 'mike' as name

    11. union all

    12. select 6 as id, 'will' as name

    13. ) t

    14. where find_in_set(t.id, '2,3,4') > 0;

  • 相关阅读:
    Hadoop功能模块之hdfs
    Hadoop介绍
    大数据的介绍
    Hadoop之shell命令
    Flume
    C# DataTable使用方法详解
    npoi 操作excell 可以下载的链接
    node.js mqtt样例
    node.js压缩
    arcgis中打印所有变量的名称和值
  • 原文地址:https://www.cnblogs.com/zhangzhiping35/p/10656302.html
Copyright © 2011-2022 走看看