zoukankan      html  css  js  c++  java
  • 用php做模糊而又精确的查找

         模糊与精确是相悖的,但今天我不是在谈什么是相悖论,看下边的提问.
    数据库结构格式如下:

    id | cid

    --------------------------------

    1| ,4,5,7,

    --------------------------------

    3| ,4,5,8,

    --------------------------------

    4| ,5,4,

    --------------------------------

    6| ,5,4,7,8,

    --------------------------------

    7| ,8,4,5,7,9,

    --------------------------------

    8| ,7,9,4,5,8,10,



    我想请教一下
    假如我post过来的cid值分别是4和5,那我如何精确的查询到id=4的这条数据?
    同理,假如我post过来的cid值分别是4,5,7,8的时候,那我如何精确的查询到id=6的这条数据?

    这里请注意:
    cid这一列的数据排列是非固定式的,没规律可循,唯一能够确认的就是不会重复出现同一个值
    还有就是用like写法and链接这种方式我试过了,不够精确.例如...where cid like'%,4,%' and cid like'%,5,%'

    解决方法如下:
    道听途说不如自己親自測試,剛剛做了一個測試,結果已經出來了
    我分别用了三种方法做测试,测试的时候是循环20000次提取数据库里的数据,结果如下:

    SQL code

    //1770 ms~1800 ms

    Select id,type From hotel where type like'%|21|%'and type like'%|101|%'and LENGTH(type)=length('|21|101|')



    SQL code

    //1760 ms~1810 ms

    Select id,type From hotel where locate('|21|',type)>0and locate('|101|',type)>0and LENGTH(type)=length('|21|101|')



    SQL code

    //CPU直接100%,瀏覽器挂掉

    Select id,type From hotel where type REGEXP concat('.*[',replace('21,101',',','|'),'].*')

  • 相关阅读:
    第五百五十二天 how can I 坚持
    第五百五十一天 how can I 坚持
    第五百五十天 how can I 坚持
    第五百四十七、八、九 how can I 坚持
    第五百四十六天 how can I 坚持
    第五百四十五天 how can I 坚持
    第五百四十四 how can I 坚持
    第五百四十一、二、三天 how can I 坚持
    第五百四十天 how can I 坚持
    MySql
  • 原文地址:https://www.cnblogs.com/see7di/p/2239662.html
Copyright © 2011-2022 走看看