IPage分页总数的sql是怎么来的,为啥分页总数total和我的数据总量不一致

1. 坑: 为啥我分页查询出来的数据总量和分页总数不一致

很简单的分页查询再现:

 public PaginationResultDto<EmergencyFileResultDto> queryTaskFiles(PagingQueryDto<EmergencyQueryDto> req) {
        EmergencyQueryDto params = req.getParams();
        if (params == null) {
            params = new EmergencyQueryDto();
        }
        IPage<EmergencyFileResultDto> page = new Page<>(req.getPage(), req.getLimit());
        page = airlineApplyManager.queryTaskFiles(params, page);
        return PaginationResultDto.success(page.getRecords(), page.getTotal());
    }

 <select id="queryTaskFiles" resultType="com.nimbus.lowaltitude.dto.airline.EmergencyFileResultDto">
        select
        afef.*
        from airline_apply aa
        left join airline_flight_execute afe on aa.id = afe.apply_id
        left join airline_flight_execute_file afef on afe.id = afef.flight_execute_id
        <where>
            <if test="queryDto.demandApplyId != null and queryDto.demandApplyId != ''">
                aa.sz_happy_id = #{queryDto.demandApplyId}
            </if>
        </where>
        order by afef.take_time desc
    </select>
       

入参:

{
  "limit": 10,
  "page": 1,
  "params": {
    "demandApplyId": "961"
  }
}

结果:我sql查询出的数据总量明明是19个,为啥total是1!!
在这里插入图片描述
难道不应该是
在这里插入图片描述
为啥???

2. 原因分析

当查询条件只与主表有关系时候,查询总数不会关联子表,要想实现上述效果,必须在where条件中涉及子表,或者在select 后加 distinct

3. 问题解决

 <select id="queryTaskFiles" resultType="com.nimbus.lowaltitude.dto.airline.EmergencyFileResultDto">
        select
        afef.*
        from airline_apply aa
        left join airline_flight_execute afe on aa.id = afe.apply_id
        left join airline_flight_execute_file afef on afe.id = afef.flight_execute_id
        <where>
            <if test="queryDto.demandApplyId != null and queryDto.demandApplyId != ''">
                aa.sz_happy_id = #{queryDto.demandApplyId} and afef.model_status = 1
            </if>
        </where>
        order by afef.take_time desc
    </select>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员码小跳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值