zoukankan      html  css  js  c++  java
  • mysql模糊查询

    Mysql 使用内置函数进行模糊查询(locate,position,instr,find_in_set)

    • 1、LOCATE('substr',str,pos)方法
    • 2、POSITION('substr' IN `field`)方法
    • 3、INSTR(`str`,'substr')方法
    • 4、FIND_IN_SET(str1,`field`)方法

    1、LOCATE('substr',str,pos)方法

        解释:返回 substr 在 str 中第一次出现的位置。如果 substr 在 str 中不存在,返回值为 0,如果substr 在 str 中存在,返回值为substr 在 str中第一次出现的位置。

                    如果pos存在,返回 substr 在 str 第pos个位置后第一次出现的位置。
        实例:

    • select locate('Light','ight_20'); 返回0
    • select locate('Light','Light_20'); 返回1
    • select locate('Light','test_Light_20'); 返回6
    • select locate('Light','Light_20',3); 返回0   
    • select locate('Light','test_Light_20'); 返回6

            例如数据库中字段scene_name有HDR_Light_20,Normal_Light_16,Light_12三种
                select * from VTD_RESULT where locate('Light',scene_name)>0;
                select * from VTD_RESULT where locate('Light',scene_name,3)>0; 

        备注:Light是要搜索的内容,scene_name为被匹配的字段,查询出所有存在scene_name的数据

                1.查出HDR_Light_20,Normal_Light_16,Light_12数据

                2.查出HDR_Light_20,Normal_Light_16数据

    2、POSITION('substr' IN `field`)方法

        这个方法可以理解为locate()方法的别名,因为它和locate()方法的作用是一样的。
        实例:
            例如数据库中字段scene_name有HDR_Light_20,Normal_Light_16,Light_12三种
                select * from VTD_RESULT where position('Light' in scene_name)>0; 查出HDR_Light_20,Normal_Light_16,Light_12数据

    3、INSTR(`str`,'substr')方法

        实例:select * from VTD_RESULT where instr(scene_name,'Light')>0;

    除了上述的方法外,还有一个函数FIND_IN_SET,这个方法比较特殊,他所查询的必须要是以“,”分隔开。

    4、FIND_IN_SET(str1,`field`)方法

        返回`field`中str1所在的位置索引,其中`field`必须以","分割开。

  • 相关阅读:
    Cordova Android源代码分析系列一(项目总览和CordovaActivity分析)
    codeforces A. Supercentral Point 题解
    Codeforces441A_Valera and Antique Items(水题)
    POJ 2407 Relatives 欧拉函数题解
    用Qt制作的Android独立游戏《吃药了》公布
    Webserver管理系列:12、开启关闭Ping命令
    高性能 Socket 组件 HP-Socket v3.2.1-RC3 公布
    00092_字符输出流Writer
    np.vstack()和np.hstack()
    用 Python 检验数据正态分布的几种方法
  • 原文地址:https://www.cnblogs.com/ting152/p/12454032.html
Copyright © 2011-2022 走看看