Java 中 mybatis 的 SQL 语句 分组 分割 字符串

SQL 语句 分组 分割 字符串

下面是 mybatis 中的一个例子

<!-- count-select -->
<select id="countByYKenan" parameterType="java.lang.String" resultType="com.YKenan.YKenan.pojo.Counts">
    select
    <if test="groupBy != null and groupBy != ''">
        SUBSTRING_INDEX(${groupBy}, '_', 1) as tName,
    </if>
    count(0) as tCounts
    from y_kenan
    where y_kenan_one like #{yKenanOne,jdbcType=VARCHAR}
    <if test="yKenanTwo != null and yKenanTwo != ''">
        AND y_kenan_two = #{yKenanTwo,jdbcType=VARCHAR}
    </if>
    <if test="yKenanThree != null and yKenanThree != ''">
        AND y_kenan_three = #{yKenanThree,jdbcType=VARCHAR}
    </if>
    <if test="yKenanFour != null and yKenanFour != ''">
        AND y_kenan_four = #{yKenanFour,jdbcType=VARCHAR}
    </if>
    <if test="groupBy != null and groupBy != ''">
        GROUP by SUBSTRING_INDEX(${groupBy}, '_', 1)
    </if>
</select>

  1. Counts 类中的属性要与 sql 输出的列 as 后面的名字及数量都要相同
  2. SUBSTRING_INDEX(str,delim,count) 此 sql 函数是指:
    (1) str 指 要进行处理的字符串
    (2) delim 指 要以什么符号进行分割
    (3) count 指 要以此符号分割的数量

例子:

在这里插入图片描述在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值