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|...';

猜你喜欢

转载自www.cnblogs.com/liaoxun/p/10318264.html