zoukankan      html  css  js  c++  java
  • mysql 定位字符串的位置

    mysql中没有charinde,使用find_in_set又只能定位逗号隔开的字符串位置,如果想查指定字符串中是否存在一个指定的字符,除了用like+count(1)以外,还可以考虑用

    locate,position,instr函数,具体使用方法:

    MySQL使用内置函数来进行模糊搜索(locate()等)

     

    常用的一共有4个方法,如下:

    1. 使用locate()方法

            1.1.普通用法:

                  SELECT `column` from `table` where locate('keyword', `condition`)>0

             类似于 java 的 indexOf();不过 locate() 只要找到返回的结果都大于0(即使是查询的内容就是最开始部分),没有查找到才返回0;

            1.2. 指定其实位置:

                   SELECT LOCATE('bar', 'foobarbar',5);  --> 7 (从foobarbar的第五个位置开始查找)

    2.使用instr()函数 (据说是locate()的别名函数)

           SELECT `column` from `table` where instr(`condition`, ‘keyword’ )>0

           唯一不同的是 查询内容的位置不同,见SQL语句中过的keyword

    3.使用position()方法,(据说也是locate()方法的别名函数,功能一样) 

             SELECT `column` from `table` where position(‘keyword’ IN `condition`)

             不过它不再是通过返回值来判断,而是使用关键字 in

    4.使用find_in_set()函数

           如: find_in_set(str,strlist),strlist必须要是以逗号分隔的字符串

        如果字符串str是在的strlist组成的N子串的字符串列表,返回值的范围为1到N

    SQL> SELECT FIND_IN_SET('b','a,b,c,d');
    +---------------------------------------------------------+
    | SELECT FIND_IN_SET('b','a,b,c,d')                       |
    +---------------------------------------------------------+
    | 2                                                       |
    +---------------------------------------------------------+
    1 row in set (0.00 sec) 

    转自:http://www.cnblogs.com/tommy-huang/p/4483583.html

  • 相关阅读:
    用Visual C#创建Windows服务程序
    C# WinForm窗口最小化到系统托盘
    C# ?? 运算符是什么?
    linux中守护进程启停工具start-stop-daemon
    linux shell脚本中 mode=${1:-sart} filename=${fileuser:-"filename"}
    Unix/Linux 脚本中 “set -e” 的作用
    利用Sonar定制自定义扫描规则
    docker 镜像详解
    Docker Compose使用
    docker搭建gitlab、Redmine
  • 原文地址:https://www.cnblogs.com/liu-shiliu/p/7084719.html
Copyright © 2011-2022 走看看