美文网首页
mysql替代like模糊查询的方法

mysql替代like模糊查询的方法

作者: 意大利大炮 | 来源:发表于2018-11-08 13:16 被阅读0次

    模糊查询几种方式

    • like语句
      SELECT 'column' FROM 'table' where 'condition' like "%keyword%"
    • LOCATE语句
      SELECT 'column' from 'table' where locate('keyword', 'condition')>0
    • locate 的別名 position
      SELECT 'column' from 'table' where position('keyword' IN 'condition')
    • INSTR语句
      SELECT 'column' from 'table' where instr('condition', 'keyword' )>0

    总结

    • locatepositioninstr 的差別只是参数的位置不同,同时locate 多一个起始位置的参数外,两者是一样的。

    • 有两种用法:一种是前面参数写变量,后面写列名;还有就是位置调换。两种有不同的效果,instr(str1,str2)的意思是str2str1中,如果后面写变量前面写列名,则表示搜出表中所有str1列中值包含str2变量的数据,这时候跟(列名 like '目标表里')的效果是一样的;

    • instr还有一个需要注意的地方,就是对该函数返回结果值的判断,不写时默认是判断 "> 0";共有三种可能,每种对应情况如下:

      1. instr(title,'name')>0 相当于 title like '%name%'
      2. instr(title,'name')=1 相当于 title like 'name%'
      3. instr(title,'name')=0 相当于 title not like '%name%'

    相关文章

      网友评论

          本文标题:mysql替代like模糊查询的方法

          本文链接:https://www.haomeiwen.com/subject/ybfwxqtx.html