求助php搜索mysql数据过滤包含指定内容

511
@Ta 2022-09-04发布,2022-09-04修改 4853点击
$res="select * from update_record where ( code='.$dh.' or code='.$dh.PRE' ) and edition='.$m_max_bb.'";


老哥们,我这段搜索里面怎么加入一个过滤包含数据的,比如过滤"edition"表名包含"13_"内容的,例如:
13_1
12_1
13_2
12_2
这样就只搜索出12_1+12_2
回复列表(19|隐藏机器人聊天)
  • @Ta / 2022-09-04 / /

    这样?

    • 不包含:edition NOT LIKE '%13\_%'
    • 不以xx开头:edition NOT LIKE '13\_%'
  • @Ta / 2022-09-04 / /

    自己手动拼接参数,不怕SQL注入嘛

  • 511
    @Ta / 2022-09-04 / /
    @无名啊,不怕😂有过滤的,我拼接后直接啥都搜索不到了,不管放前面还是后面
  • @Ta / 2022-09-04 / /

    @511,你放放拼接后的SQL出来呗

    不相干的:按理说,同样的SQL,可以复用之前解析编译好的语句缓存,应该会比自己拼接参数的SQL快些

  • 511
    @Ta / 2022-09-04 / /
    @无名啊
    $res="select * from update_record where ( code='.$dh.' or code='.$dh.PRE' ) and edition='.$m_max_bb.' and edition NOT LIKE '13\_%' ";
  • @Ta / 2022-09-04 / /

    @511,我没看出来什么问题

    但按理说,你这个SQLand edition NOT LIKE '13\_%'是多余的条件,

    因为edition='.$m_max_bb.'.开头,就肯定不会以13_开头了,

    所以应该结果没变化才对

  • @Ta / 2022-09-04 / /
    $res="select * from update_record where ( code='.$dh.' or code='.$dh.PRE' ) and edition='.$m_max_bb.' and (instr(edition,'13_')>0 or instr(edition,'12_')>0)";
  • @Ta / 2022-09-04 / /

    @echo醉老仙,感觉楼主是想要『不以xx开头』的功能,因为他在 5楼 选择了13\_%的形式

  • 511
    @Ta / 2022-09-04 / /
    @无名啊@echo醉老仙,搞定了,无名那个办法就可以,是我用错了
  • @Ta / 2022-09-04 / /

    @511,<=0就行了,据说instr比like快

  • 511
    @Ta / 2022-09-04 / /
    @echo醉老仙,的确效率比较高😂你那段就是过滤包含12_跟13_的嘛,后面说的<=是啥作用的
  • @Ta / 2022-09-04 / /

    @511@echo醉老仙,你们说的是instrLIKE '%xxx%'快嘛?还是LIKE 'xx%'

  • 511
    @Ta / 2022-09-05 / /
    @echo醉老仙@无名啊,老哥,还有个小问题,时间戳怎么在我指定时间加上去😂指定在明年的今天或者下个月的今天,加5年这个样子
  • 511
    @Ta / 2022-09-05 / /
    @无名啊,instr效率比like高
  • @Ta / 2022-09-05 / /

    @511

    instr效率比like高

    你这个例子中,不是只需要“不以xx开头”嘛?

    这种情况,instr 应该满足不了你,而且 LIKE 'xxx%' 的效率很高

    如,xxx LIKE 'hello%' 时,是可以优化成 xxx >= 'hello' AND xxx < 'hellp'xxx NOT LIKE 则为 xxx < 'hello' OR xxx >= 'hellp'

    连 1MB 的 SQLite 都能做这个优化,我默认所有数据库都这么做了

    instr 应该是扫表后,对每一行进行计算的

  • @Ta / 2022-09-05 / /

    @511,时间的加法如果在php中比较简单,
    加一个月:strtotime("+1 months",strtotime('2022-09-05 18:28:23'))
    加5天:strtotime("+5 days",strtotime('2022-09-05 18:28:23'))
    加5年:strtotime("+5 year",strtotime('2022-09-05 18:28:23'))
    你甚至可以:strtotime("+1 week 2 days 4 hours 2 seconds",strtotime('2022-09-05 18:28:23'))
    得到结果后用:date('Y-m-d H:i:s',datetime)重新转为日期时间
    比如:date('Y-m-d H:i:s',strtotime("+5 year",strtotime('2022-09-05 18:28:23')))

  • @Ta / 2022-09-05 / /

    @511@echo醉老仙MySQL 有专门的时间加法函数 date_add

    官方文档写得很详细。或者看 MySQL 的书籍应该也有

    要不百度啥的,资料也很多(暗示

  • 511
    @Ta / 2022-09-06 / /
    @echo醉老仙,OK,我思路用错了
  • 511
    @Ta / 2022-09-07 / /
    @echo醉老仙,兄得,php获取时间戳慢8个小时,我php.ini已经改成
    data.timezone=PRC
    ,还有在php开头用
    data_default_timezone_set('PRC');
    ,结果都一样,还是慢了8个小时,唯一解决办法就是在获取出来的时间戳+28800才可以准确😂你知道啥原因吗
添加新回复
回复需要登录