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>