經常會有模糊查詢的需求,寫個模糊查詢其實很簡單,但是其中可能會遇到一些極少見的問題。例如萬用字元問題。
模糊查詢如下:
//查詢param相似的引數,例如1param和param3都會匹配到
//第一種寫法:
select s.iid,s.word from sensitive_word s where s.word
like concat(concat('%','param'),'%')
//第二種寫法
select s.iid,s.word from sensitive_word s
where s.word like concat('%','param','%');
//mybatis中的xml配置寫法
resultmap="sensitivewordresultresult">
select s.iid,s.word from sensitive_word s where
s.word like concat(concat('%',#),'%') and
if>
s.is_deleted = 0 order by s.update_time desc
select>
萬用字元的問題,例如輸入的引數是』%』或『_』,則會匹配到所有的資料,則當然不是我們需要的結果,所以,可用轉義字元的方式對這倆字段進行處理。
sql語句中可進行如下處理:在』%』前加『\』即可,即如下:
select s.iid,s.word,s.create_time from sensitive_word s
where s.word like concat(concat('%','\%'),'%') ;
對於mybatis中的使用,在xml配置裡加上這個比較麻煩,則可在業務**裡對入參進行處理,比較方便,加如下**即可:
if(sensitiveword.contains("%") || sensitiveword.contains("_"))
相當於人為給字元前加上轉義字元。也就避免了萬用字元的濫用尷尬。 解決模糊查詢速度慢問題 ORACLE
乙個困擾我良久的問題,在今天早上8 41分時基本解決。資料庫的資料量非常龐大,查詢速度極慢,建立索引後 搜尋速度問題解決,但是在搜尋的sql語句中大部分是用like,可是like 是不使用索引的,而like 則經過索引,求教曾經的老師 高階程式設計師也無用,在 上搜尋 求助,最終鎖定兩種辦法 1.全...
MySQL in 模糊查詢問題的解決方法
表資料 查詢sql substring index substring index 需要拆分為行的字串,help topic id 1 1 as content from mysql.help topic where help topic id length 需要拆分為行的字串 length rep...
mysql模糊查詢索引失效問題及解決方案
我們在使用like 萬用字元時常常會引起索引失效的問題。這裡,我們討論一下like使用 的幾種情況 下列例子用到的索引 vc student name 一 like xx explain select from t student where vc student name like 王 二 lik...