zoukankan      html  css  js  c++  java
  • 【MySQL】MySQL的find_in_set的使用例子

    > 参考的优秀文章

    FIND_IN_SET(str,strlist)

    > 简单的例子

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

    -- 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('2', '');
    -- 0 select find_in_set('2', '1,2 ,3,4,5,6');
    -- NULL select find_in_set(NULL, '1,2,3,4,5,6');
    -- NULL select find_in_set(1, NULL);
    -- 0 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 
    (
    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;
    View Code
  • 相关阅读:
    Python 函数 之 目录
    python---------匿名函数
    python-------递归函数
    python-----内置函数
    hibernate.cfg.xml
    struts2 工作原理
    拦截器
    js制作 子菜单
    struts---最简单实例步骤
    常用标签---地址----
  • 原文地址:https://www.cnblogs.com/nick-huang/p/5132104.html
Copyright © 2011-2022 走看看