打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
【mysql 中的模糊查询】

原文链接:http://www.cnblogs.com/cyttina/p/3894428.html

http://blog.sina.com.cn/s/blog_667bef380101f2da.html

1.  参数中直接加入%%

  param.setUsername("%CD%");
      param.setPassword("%11%");

	<select  id="selectPersons" resultType="person" parameterType="person">		select id,sex,age,username,password from person where true 			<if test="username!=null"> AND username LIKE #{username}</if>			<if test="password!=null">AND password LIKE #{password}</if>		</select>

2.  bind标签

<select id="selectPersons" resultType="person" parameterType="person">  <bind name="pattern" value="'%' + _parameter.username + '%'" />  select id,sex,age,username,password   from person  where username LIKE #{pattern}</select>

 

 

3. CONCAT

where username LIKE concat(concat('%',#{username}),'%')


模糊查询:

工作中用到,写三种用法吧,第四种为大小写匹配查询

 

1. sql中字符串拼接

   SELECT * FROM tableName WHERE name LIKE CONCAT(CONCAT('%', #{text}), '%');

 

2. 使用 ${...} 代替 #{...}

   SELECT * FROM tableName WHERE name LIKE '%${text}%'; 

 

3. 程序中拼接

   Java

   // or String searchText = "%" + text + "%";

   String searchText = new StringBuilder("%").append(text).append("%").toString();

   parameterMap.put("text", searchText);

 

   SqlMap.xml

   SELECT * FROM tableName WHERE name LIKE #{text};

 

4. 大小写匹配查询

  1. SELECT *  FROM TABLENAME  WHERE UPPER(SUBSYSTEM) LIKE '%' || UPPER('jz') || '%'  
  2.  --或者是  
  3. SELECT *   FROM TABLENAME  WHERE LOWER(SUBSYSTEM) LIKE '%' || LOWER('jz') || '%' 





最后一个是在自己写的时候,由于关键字为空,查询不出来的情况



<select id="queryPassMember" parameterType="map" resultMap="BaseResultMap">		select		<include refid="Base_Column_List" />		from shop_employee_relation		<where>			<if test="shopId != null">				SHOP_ID= #{shopId}				and delete_flag=1			</if>			<if test="mobile !=null and mobile !=''">				AND shopkeeper_phone LIKE				concat(concat('%',#{mobile}),'%')			</if>			<if test="memberName !=null and memberName !=''">				AND member_name LIKE concat(concat('%',#{memberName}),'%')			</if>		</where>		order by ROLE asc	</select>
                                                           题外话:今天下午状态突然下降,有点饿,坐等下班中。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
MyBatis学习 之 三、动态SQL语句
mybatis 动态SQL
Mybatis 动态sql语句if标签和where标签结合使用
一些绕过waf的笔记
mysql函数之四:concat() mysql 多个字段拼接
SQL手工注入总结 必须收藏
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服