解决PageHelp 在获取 count 的时候 过滤 order by 导致 总数不正确问题

10 篇文章 0 订阅
7 篇文章 0 订阅

pageHelp 获取count的时候会默认把order by 过滤掉,有可能会导致实际总数与查出来的总数不一样的情况,如下面的SQL:

SELECT a.*
FROM (
SELECT card , name , sex , activity_level , store_name , member_id , trade_total , create_card_time
FROM report_member_advance
WHERE company_id = #{companyId} AND store_id = #{storeId}
ORDER BY id DESC
) a
GROUP BY a.member_id
HAVING 1 = 1
<if test="quantity != null and quantity == 5">
    -- 大于4次
    AND trade_total <![CDATA[ > ]]> 4
</if>
<if test="quantity != null and quantity == 99">
    -- 消费总数
    AND trade_total <![CDATA[<>]]> 0
</if>
<if test="quantity != null and quantity != 99 and quantity != 5">
    -- 0-4次
    AND trade_total = #{quantity}
</if>

pageHelp 查出来的count 是103,复制sql执行是101,通过跟踪源码得知,作者是做了后门的,只要在需要不过滤的order by 前面 添加  /*keep orderby*/ 注解后,pageHelp 就会忽略不过滤order by,亲测有效~~,最终sql为

SELECT a.*
FROM (
SELECT card , name , sex , activity_level , store_name , member_id , trade_total , create_card_time
FROM report_member_advance
WHERE company_id = #{companyId} AND store_id = #{storeId}
/*keep orderby*/ORDER BY id DESC
) a
GROUP BY a.member_id
HAVING 1 = 1
<if test="quantity != null and quantity == 5">
    -- 大于4次
    AND trade_total <![CDATA[ > ]]> 4
</if>
<if test="quantity != null and quantity == 99">
    -- 消费总数
    AND trade_total <![CDATA[<>]]> 0
</if>
<if test="quantity != null and quantity != 99 and quantity != 5">
    -- 0-4次
    AND trade_total = #{quantity}
</if>
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值