sql中遇到多个or条件还伴随着like的全模糊查询怎么办?

示例:

select * from a where a.a like concat('%','123','%') or a.b like  concat('%','123','%') or a.c like concat('%','123','%');

或者

select * from a where a.a like concat('%','123','%') or a.b like  concat('%','234','%') or a.c like concat('%','456','%');

这种执行起来效率不高,怎么办?这里介绍一下instr(str1,str2)这个函数,是找str2在str1中出现几次,如果没出现则返回0,如果出现了则大于1

提供以下办法,真的第一种情况有两种办法,第一种先将a,b,c字段拼接起来,然后再like,这样会稍微快一点,如下(注意concat函数需要注意null值):

select * from a where concat(ifnull(a.a,''),ifnull(a.a,''),ifnull(a.a,'')) like concat('%','123','%');

第二种:

select * from a where instr(a.a,'123')>0 or instr(a.b,'123')>0 or instr(a.c'123')>0;

第三种就是将第一种和第二种集合起来

select * from a where instr(concat(ifnull(a.a,''),ifnull(a.b,''),ifnull(a.c,'')) ,'123');

当然遇到第二种情况就老老实实用instr函数吧,会比上面的快一点

select * from a where instr(a.a,'123')>0 or instr(a.b,'123')>0 or instr(a.c'456')>0;

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值