left join 导致的分页错误

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

本文主要是介绍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

相关文章

安卓链接正常显示,ios#符被转义%23导致链接访问404

原因分析: url中含有特殊字符 中文未编码 都有可能导致URL转换失败,所以需要对url编码处理  如下: guard let allowUrl = webUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {return} 后面发现当url中有#号时,会被误伤转义为%23,导致链接无法访问

oracle分页和mysql分页

mysql 分页 --查前5 数据select * from table_name limit 0,5 select * from table_name limit 5 --limit关键字的用法:LIMIT [offset,] rows--offset指定要返回的第一行的偏移量,rows第二个指定返回行的最大数目。初始行的偏移量是0(不是1)。   oracle 分页 --查前1-9

STM32 ADC+DMA导致写FLASH失败

最近用STM32G070系列的ADC+DMA采样时,遇到了一些小坑记录一下; 一、ADC+DMA采样时进入死循环; 解决方法:ADC-dma死循环问题_stm32 adc dma死机-CSDN博客 将ADC的DMA中断调整为最高,且增大ADCHAL_ADC_Start_DMA(&hadc1, (uint32_t*)adc_buffer, ADC_Buffer_Size); 的ADC_Bu

【经验交流】修复系统事件查看器启动不能时出现的4201错误

方法1,取得『%SystemRoot%\LogFiles』文件夹和『%SystemRoot%\System32\wbem』文件夹的权限(包括这两个文件夹的所有子文件夹的权限),简单点说,就是使你当前的帐户拥有这两个文件夹以及它们的子文件夹的绝对控制权限。这是最简单的方法,不少老外说,这样一弄,倒是解决了问题。不过对我的系统,没用; 方法2,以不带网络的安全模式启动,运行命令行,输入“ne

DAY16:什么是慢查询,导致的原因,优化方法 | undo log、redo log、binlog的用处 | MySQL有哪些锁

目录 什么是慢查询,导致的原因,优化方法 undo log、redo log、binlog的用处  MySQL有哪些锁   什么是慢查询,导致的原因,优化方法 数据库查询的执行时间超过指定的超时时间时,就被称为慢查询。 导致的原因: 查询语句比较复杂:查询涉及多个表,包含复杂的连接和子查询,可能导致执行时间较长。查询数据量大:当查询的数据量庞大时,即使查询本身并不复杂,也可能导致

SQL2005 性能监视器计数器错误解决方法

【系统环境】 windows 2003 +sql2005 【问题状况】 用户在不正当删除SQL2005后会造成SQL2005 性能监视器计数器错误,如下图 【解决办法】 1、在 “开始” --> “运行”中输入 regedit,开启注册表编辑器,定位到 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVer

71-java 导致线程上下文切换的原因

Java中导致线程上下文切换的原因通常包括: 线程时间片用完:当前线程的时间片用完,操作系统将其暂停,并切换到另一个线程。 线程被优先级更高的线程抢占:操作系统根据线程优先级决定运行哪个线程。 线程进入等待状态:如线程执行了sleep(),wait(),join()等操作,使线程进入等待状态或阻塞状态,释放CPU。 线程占用CPU时间过长:如果线程执行了大量的I/O操作,而不是CPU计算

ssm 之事务管理出现错误

JDBC Connection will not be managed by Spring 项目采用的是分布式架构,分别有controller,service,solr三个服务器,之间通过dubbo进行调用,经过测试发现事务配置完以后不能通过spring进行管理,其中两条insert和一条update语句都执行完毕,异常并没有使得事务进行回滚,通过调取debug日志发现“JDBC Conn

Unstructured cannot write mode RGBA as JPEG 错误解决

Unstructured cannot write mode RGBA as JPEG 错误解决 0. 错误详细1. 解决方法 0. 错误详细 Image Extraction Error: Skipping the failed imageTraceback (most recent call last):File "/root/miniconda3/envs/learn-y

收藏:解决 pip install 出现 error: subprocess-exited-with-error 错误的方法

在使用 pip 安装 Python 包时,有时候会遇到 error: subprocess-exited-with-error 错误。这种错误通常是由于 setuptools 版本问题引起的。本文将介绍如何解决这一问题 当你使用 pip install 安装某个 Python 包时,如果 setuptools 版本过高或过低,可能会导致安装过程出错,并出现类似以下错误信息:error: subpr