Mybatis框架中SQL之一

查询innerCode字段第indexStar到indexend的字符的最大值

<select id="findOrgInCodeMax" parameterType="java.util.Map" resultMap="BaseResultMap">
select MAX(substring(innerCode,#{indexstart},#{indexend})) as innerCode,orgCode,orgName from meteor_org
</select>

更新语句

<update id="update" parameterType="org.meteor.mgr.org.entity.OrgEntity">
  update meteor_org 
  <set>
  <if test="orgCode != null">
    orgCode=#{orgCode,jdbcType=VARCHAR},
  </if>
  <if test="innerCode != null">
    innerCode=#{innerCode,jdbcType=VARCHAR},
  </if>
   </set>
   where orgId=#{orgId,jdbcType=CHAR}
</update>

去掉重复数据分为,部分字段的数据重复和所有字段的数据都重复

部分字段的值重复就要用group by 

		SELECT MAX(roleId),userId,roleMenuId,aa.menuId,aa.menuCode,aa.menuName,aa.menuUrl,aa.menuIcon,aa.parentId,aa.enableFlag FROM (
	SELECT userId,a.roleId,roleMenuId,b.menuId,c.menuCode,c.menuName,c.menuUrl,c.menuIcon,c.parentId,c.enableFlag FROM meteor_user_role AS a 
	left JOIN meteor_role_menu as b ON a.roleId=b.roleId 
LEFT JOIN meteor_role as d ON a.roleId=d.roleId
	LEFT JOIN meteor_menu as c ON c.menuId=b.menuId
	WHERE userId=#{userId,jdbcType=CHAR} and c.enableFlag='1' and d.enableFlag='1') as aa
	GROUP BY aa.menuId

所有字段的值都重复则可以用distinct关键字

比如 select   distinct name from table  ,具体可以参考https://www.cnblogs.com/shiluoliming/p/6604407.html

分页查询和根据一个值查询多个字段,把需要查询的字段写在第一个CONCAT()里面

<select id="findBysearchStr" resultMap="BaseResultMap" parameterType="org.meteor.mgr.entity.Criteria">
	select * from dlhd_contractscope where 1=1
	<if test="condition != null and condition != ''"> 
   and CONCAT(contractScopeCode,contractScopeName) LIKE CONCAT('%',#{condition},'%') 
    </if> 
     <if test="orderByClause != null" >
	      order by ${orderByClause}
	    </if>
    	<if test="mysqlOffset != null and mysqlLength != null" >
		   <![CDATA[ limit #{mysqlOffset} , #{mysqlLength} ]]>
		</if>
</select>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值