mysql 實行模糊查詢 一個輸入值匹配多個字段和多個輸入值匹配一個字段


mysql 實行模糊查詢  一個輸入值匹配多個字段

MySQL單表多字段模糊查詢可以通過下面這個SQL查詢實現 為啥一定要150字以上  真的麻煩  還不讓貼代碼了

 

SELECT * FROM `magazine` WHERE CONCAT(`title`,`tag`,`description`) LIKE ‘%關鍵字%’

實例:

    select * from mcode_specific_information where 1=1
              <if test="typeID!=null and typeID !=''">
                  and typeID like "%"#{typeID}"%" 
              </if>
              <if test="typeName!=null  and typeName !=''">
                  and typeName like "%"#{typeName}"%" 
              </if>
              <if test="entryUnit!=null  and entryUnit !=''">
                  and entryUnit like "%"#{entryUnit}"%" 
              </if>
              <if test="alias!=null  and alias !=''">
                  and CONCAT(`alias`,`alias1`,`alias2`) like "%"#{alias}"%" 
              </if>
              <if test="alias1!=null  and alias1 !=''">
                  and CONCAT(`alias`,`alias1`,`alias2`)  like "%"#{alias1}"%" 
              </if>
              <if test="alias2!=null  and alias2 !=''">
                  and CONCAT(`alias`,`alias1`,`alias2`)  like "%"#{alias2}"%" 
              </if>
              

 當一個字段想模糊查詢出多個字段的時候,正常情況下一般會這么作

1 select * from a where name like 'a%' or name like 'b%' ....or ...;

但是上面的情況只能對應少量的模糊查詢值,過多之后再后台開發的時候會出現非常麻煩的sql語句拼接

這時我們可以采用正則表達式進行匹配

1 select * from a where name regexp'a|b|...';

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM