left join 导致的分页错误

2024-08-21 09:48
文章标签 错误 分页 join 导致 left

本文主要是介绍left join 导致的分页错误,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 行转列解决
  • 先分页再组装

背景:有规则表t_rule,标签表t_label,中间表t_rule_label,根据t_rule的name和t_label表的name组合查询规则

t_rule表数据

idname
1rule1
2rule2

t_label表数据

idname
1label1
2label2

t_rule_label表数据

rule_idlabel_id
11
12
21
22

使用mybatis-plus 查询

Page<CheckRule> page = new Page<>(query.getPageNum(), query.getPageSize());
select rule.id,rule.name,label.id label_id,label.name label_name
from t_rule rule
left join t_rule_label con on con.rule_id = rule.id
left join t_label label on label.id = con.label_id
where rule.name like '%ru%' and label.id in ('1','2')

left join 导致查询出4条数据
在这里插入图片描述

page.getTotal() 和 page.getRecords() 对应不上
因为一对多导致getTotal()数量比getRecords()多,getRecords()为resultMap 组合之后的数据

<resultMap id="ruleResultMap" type="com.yss.rule.entity.Rule"><id column="id" property="id" /><result column="name" property="name" /><collection property="labelList" ofType="com.yss.rule.entity.Label" ><id column="lable_id" property="id" /><result column="lable_name" property="name" /></collection></resultMap><select id="getRules" resultMap="rulListMap">select rule.id,rule.name,label.id label_id,label.name label_namefrom t_rule ruleleft join t_rule_label con on con.rule_id = rule.idleft join t_label label on label.id = con.label_id<where><if test="ruleAndLabelVo.ruleName !=null and ruleAndLabelVo.ruleName !=''">and rule.name like '%${ruleAndLabelVo.ruleName }%'</if><if test="ruleAndLabelVo.labelIds !=null and ruleAndLabelVo.labelIds.size() !=0">and lable.id in<foreach collection="ruleAndLabelVo.labelIds" index="index" item="item" open="(" separator="," close=")">#{item}</foreach></if></where>                     
</select>

行转列解决

对于oracle 行转列

wmsys.wm_concat,oracle 10g推出的函数,12c以后被去掉 select
id,wmsys.wm.concat(name) lable_name from t_label group by id

LISTAGG,是oracle11g推出的函数 select id, listagg(lable_name,’,’) within
group (order by lable_name) lable_name from t_label group by id;

对于mysql 行转列

SELECT GROUP_CONCAT(cast(user_id as char(10)) SEPARATOR ‘,’) as id
from user;

先分页再组装

public class RuleAndLabelVo{//前端传递条件private String ruleName;private List<String> labelIds;//第一次查询出的t_rule的主键集合private List<String> ruleIds;
}

第一次查询,分页查询,查询出t_rule的主键集合

<select id="getRulesPage" resultMap="string">select distinct rule.idfrom t_rule ruleleft join t_rule_label con on con.rule_id = rule.idleft join t_label label on label.id = con.label_id<where><if test="ruleAndLabelVo.ruleName !=null and ruleAndLabelVo.ruleName !=''">and rule.name like '%${ruleAndLabelVo.ruleName }%'</if><if test="ruleAndLabelVo.labelIds !=null and ruleAndLabelVo.labelIds.size() !=0">and lable.id in<foreach collection="ruleAndLabelVo.labelIds" index="index" item="item" open="(" separator="," close=")">#{item}</foreach></if></where>  order by rule.id
</select>

第二次查询,将t_rule的主键集合带入查询

List ruleIds = checkRuleMapper.getRulesPage(page,
ruleByRuleOrLabelVo); ruleAndLabelVo.setRuleIds(ruleIds);

<resultMap id="ruleResultMap" type="com.yss.rule.entity.Rule"><id column="id" property="id" /><result column="name" property="name" /><collection property="labelList" ofType="com.yss.rule.entity.Label" ><id column="lable_id" property="id" /><result column="lable_name" property="name" /></collection></resultMap><select id="getRules" resultMap="rulListMap">select rule.id,rule.name,label.id label_id,label.name label_namefrom t_rule ruleleft join t_rule_label con on con.rule_id = rule.idleft join t_label label on label.id = con.label_id<where><if test="ruleAndLabelVo.ruleIds !=null and ruleAndLabelVo.ruleIds.size() !=0">and rule.id in<foreach collection="ruleAndLabelVo.ruleIds" index="index" item="item" open="(" separator="," close=")">#{item}</foreach></if></where>                     
</select>

这篇关于left join 导致的分页错误的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1092814

相关文章

Windows Docker端口占用错误及解决方案总结

《WindowsDocker端口占用错误及解决方案总结》在Windows环境下使用Docker容器时,端口占用错误是开发和运维中常见且棘手的问题,本文将深入剖析该问题的成因,介绍如何通过查看端口分配... 目录引言Windows docker 端口占用错误及解决方案汇总端口冲突形成原因解析诊断当前端口情况解

MySQL高级查询之JOIN、子查询、窗口函数实际案例

《MySQL高级查询之JOIN、子查询、窗口函数实际案例》:本文主要介绍MySQL高级查询之JOIN、子查询、窗口函数实际案例的相关资料,JOIN用于多表关联查询,子查询用于数据筛选和过滤,窗口函... 目录前言1. JOIN(连接查询)1.1 内连接(INNER JOIN)1.2 左连接(LEFT JOI

C/C++错误信息处理的常见方法及函数

《C/C++错误信息处理的常见方法及函数》C/C++是两种广泛使用的编程语言,特别是在系统编程、嵌入式开发以及高性能计算领域,:本文主要介绍C/C++错误信息处理的常见方法及函数,文中通过代码介绍... 目录前言1. errno 和 perror()示例:2. strerror()示例:3. perror(

Go标准库常见错误分析和解决办法

《Go标准库常见错误分析和解决办法》Go语言的标准库为开发者提供了丰富且高效的工具,涵盖了从网络编程到文件操作等各个方面,然而,标准库虽好,使用不当却可能适得其反,正所谓工欲善其事,必先利其器,本文将... 目录1. 使用了错误的time.Duration2. time.After导致的内存泄漏3. jsO

Python中ModuleNotFoundError: No module named ‘timm’的错误解决

《Python中ModuleNotFoundError:Nomodulenamed‘timm’的错误解决》本文主要介绍了Python中ModuleNotFoundError:Nomodulen... 目录一、引言二、错误原因分析三、解决办法1.安装timm模块2. 检查python环境3. 解决安装路径问题

如何解决mysql出现Incorrect string value for column ‘表项‘ at row 1错误问题

《如何解决mysql出现Incorrectstringvalueforcolumn‘表项‘atrow1错误问题》:本文主要介绍如何解决mysql出现Incorrectstringv... 目录mysql出现Incorrect string value for column ‘表项‘ at row 1错误报错

Mysql中深分页的五种常用方法整理

《Mysql中深分页的五种常用方法整理》在数据量非常大的情况下,深分页查询则变得很常见,这篇文章为大家整理了5个常用的方法,文中的示例代码讲解详细,大家可以根据自己的需求进行选择... 目录方案一:延迟关联 (Deferred Join)方案二:有序唯一键分页 (Cursor-based Paginatio

java String.join()的使用小结

《javaString.join()的使用小结》String.join()是Java8引入的一个实用方法,用于将多个字符串按照指定分隔符连接成一个字符串,本文主要介绍了javaString.join... 目录1. 方法定义2. 基本用法2.1 拼接多个字符串2.2 拼接集合中的字符串3. 使用场景和示例3

mybatis-plus分页无效问题解决

《mybatis-plus分页无效问题解决》本文主要介绍了mybatis-plus分页无效问题解决,原因是配置分页插件的版本问题,旧版本和新版本的MyBatis-Plus需要不同的分页配置,感兴趣的可... 昨天在做一www.chinasem.cn个新项目使用myBATis-plus分页一直失败,后来经过多方

SpringBoot项目启动错误:找不到或无法加载主类的几种解决方法

《SpringBoot项目启动错误:找不到或无法加载主类的几种解决方法》本文主要介绍了SpringBoot项目启动错误:找不到或无法加载主类的几种解决方法,具有一定的参考价值,感兴趣的可以了解一下... 目录方法1:更改IDE配置方法2:在Eclipse中清理项目方法3:使用Maven命令行在开发Sprin