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

相关文章

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

前端bug调试的方法技巧及常见错误

《前端bug调试的方法技巧及常见错误》:本文主要介绍编程中常见的报错和Bug,以及调试的重要性,调试的基本流程是通过缩小范围来定位问题,并给出了推测法、删除代码法、console调试和debugg... 目录调试基本流程调试方法排查bug的两大技巧如何看控制台报错前端常见错误取值调用报错资源引入错误解析错误

部署Vue项目到服务器后404错误的原因及解决方案

《部署Vue项目到服务器后404错误的原因及解决方案》文章介绍了Vue项目部署步骤以及404错误的解决方案,部署步骤包括构建项目、上传文件、配置Web服务器、重启Nginx和访问域名,404错误通常是... 目录一、vue项目部署步骤二、404错误原因及解决方案错误场景原因分析解决方案一、Vue项目部署步骤

在MySQL执行UPDATE语句时遇到的错误1175的解决方案

《在MySQL执行UPDATE语句时遇到的错误1175的解决方案》MySQL安全更新模式(SafeUpdateMode)限制了UPDATE和DELETE操作,要求使用WHERE子句时必须基于主键或索引... mysql 中遇到的 Error Code: 1175 是由于启用了 安全更新模式(Safe Upd