数据库like的替代函数

SELECT `column` FROM `table` where `condition` like `%keyword%’

事实上,可以使用 locate 和 instr 这两个函数来代替

SELECT `column` from `table` where locate(‘keyword’, `condition`)>0
或是 locate 的別名 position
SELECT `column` from `table` where position(‘keyword’ IN `condition`)

或是
SELECT `column` from `table` where instr(`condition`, ‘keyword’ )>0

locate、position 和 instr 的差別只是参数的位置不同,同时locate 多一个请始位置的参数外,两者是一样的。

速度上这三个比用 like 稍快了一點。

  

当 condition 需要作为参数传递时,为了避免因为类型的原因 而查询结果错误的问题(position(‘keyword’ IN $a) keyword 为int类型时,则查询结果总是为一条) 应使用find_IN_SET代替。find_int_set(列名,$a) ; 当表中无数据或者列为null时,postion会成立 find_IN_SET 比较严格 如果没有数据 则不成立

原文地址:https://www.cnblogs.com/torchstar/p/14955992.html