【Mybatis系列】Mybatis空值关联

💝💝💝欢迎来到我的博客,很高兴能够在这里和您见面!希望您在这里可以感受到一份轻松愉快的氛围,不仅可以获得有趣的内容和知识,也可以畅所欲言、分享您的想法和见解。

  • 推荐:kwan 的首页,持续学习,不断总结,共同进步,活到老学到老
  • 导航
    • 檀越剑指大厂系列:全面总结 java 核心技术点,如集合,jvm,并发编程 redis,kafka,Spring,微服务,Netty 等
    • 常用开发工具系列:罗列常用的开发工具,如 IDEA,Mac,Alfred,electerm,Git,typora,apifox 等
    • 数据库系列:详细总结了常用数据库 mysql 技术点,以及工作中遇到的 mysql 问题等
    • 懒人运维系列:总结好用的命令,解放双手不香吗?能用一个命令完成绝不用两个操作
    • 数据结构与算法系列:总结数据结构和算法,不同类型针对性训练,提升编程思维,剑指大厂

非常期待和您一起在这个小小的网络世界里共同探索、学习和成长。💝💝💝 ✨✨ 欢迎订阅本专栏 ✨✨

博客目录

一.问题描述

1.已知条件

已知 table_1 有 3 个字段 order_no,community_id,post_id

已知 table_2 也有 3 个字段 order_no,community_id,post_id

2.关联条件

现在需要将 table_1 和 table_2 进行关联,关联条件是 order_no,community_id,post_id 这 3 个字段,但是 order_no 不为 null,不过 community_id,post_id 是可能为 null,也可能不为 null

3.初步解法

如以下 SQL 所示,发现结果为空,没有查询到数据,实际是有数据,问题在于 community_id 和 post_id 对于空值的处理。

apl 复制代码
<select id="totalPageInfo" resultType="com.kwan.springbootkwan.entity.dto.CsdnTotalIncomeDTO">
    SELECT t1.receiver_nick_name AS nickName
    , SUM(t1.received_money) AS amount
    FROM table_1 t1 left join table_2 t2
    on t1.order_no = t2.order_no
           AND t1.community_id=t2.community_id
           AND t1.post_id=t2.post_id
    WHERE 1 = 1
    <if test="query.startDate != null">
        AND t1.receive_time <![CDATA[>= #{query.startDate}]]>
        AND t2.create_time <![CDATA[>= #{query.startDate}]]>
    </if>
    <if test="query.endDate != null">
        AND t1.receive_time <![CDATA[<= #{query.endDate}]]>
        AND t2.create_time <![CDATA[<= #{query.endDate}]]>
    </if>
    GROUP BY nickName
    ORDER BY amount DESC
</select>

二.解决方案

1.SQL 如下

apl 复制代码
<select id="totalPageInfo" resultType="com.kwan.springbootkwan.entity.dto.CsdnTotalIncomeDTO">
    SELECT t1.receiver_nick_name AS nickName
    , SUM(t1.received_money) AS amount
    FROM table_1 t1 left join table_2 t2
    on t1.order_no = t2.order_no
    <![CDATA[
           AND t1.community_id   <=> t2.community_id
           AND t1.post_id<=> t2.post_id
    ]]>
    WHERE 1 = 1
    <if test="query.startDate != null">
        AND t1.receive_time <![CDATA[>= #{query.startDate}]]>
        AND t2.create_time <![CDATA[>= #{query.startDate}]]>
    </if>
    <if test="query.endDate != null">
        AND t1.receive_time <![CDATA[<= #{query.endDate}]]>
        AND t2.create_time <![CDATA[<= #{query.endDate}]]>
    </if>
    GROUP BY nickName
    ORDER BY amount DESC
</select>

2.解释说明

在这个 SQL 查询中,由于 community_id 和 post_id 可能为空,你可以通过使用 COALESCE 函数或 IFNULL 函数(具体取决于你使用的数据库系统)来处理可能的空值情况。

下面是一种修改方式,假设你使用的是 MySQL 数据库:

sql 复制代码
SELECT
    t1.receiver_nick_name AS nickName,
    SUM(t1.received_money) AS amount
FROM
    table_1 t1
LEFT JOIN
    table_2 t2 ON t1.order_no = t2.order_no
                        AND t1.community_id <=> t2.community_id
                        AND t1.post_id <=> t2.post_id
WHERE
    1 = 1
    <if test="query.startDate != null">
        AND t1.receive_time <![CDATA[>= #{query.startDate}]]>
        AND t2.create_time <![CDATA[>= #{query.startDate}]]>
    </if>
    <if test="query.endDate != null">
        AND t1.receive_time <![CDATA[<= #{query.endDate}]]>
        AND t2.create_time <![CDATA[<= #{query.endDate}]]>
    </if>
GROUP BY
    nickName
ORDER BY
    amount DESC

在这里,使用了 <=> 操作符,它在 MySQL 中用于处理 NULL 值的比较。如果 community_id 或 post_id 的其中一个是 NULL,那么 <=> 操作符会返回 true。

请根据你使用的数据库类型来调整语法。如果是其他数据库,可能会使用 COALESCE 或 IS NULL 等不同的语法。

觉得有用的话点个赞 👍🏻 呗。

❤️❤️❤️本人水平有限,如有纰漏,欢迎各位大佬评论批评指正!😄😄😄

💘💘💘如果觉得这篇文对你有帮助的话,也请给个点赞、收藏下吧,非常感谢!👍 👍 👍

🔥🔥🔥Stay Hungry Stay Foolish 道阻且长,行则将至,让我们一起加油吧!🌙🌙🌙

相关推荐
乌暮3 天前
少写重复 CRUD:用大白话搞懂 MyBatis-Plus
java·开发语言·spring boot·面试·mybatis
CoderYanger3 天前
Java EE 进阶:9.MyBatis 操作数据库(进阶)(第四弹):图书管理系统(用户登录+添加图书)
java·数据库·spring boot·mysql·面试·java-ee·mybatis
牧瀬クリスだ3 天前
从零手写博客系统:分层架构与统一响应(一)
java·数据库·架构·mybatis·springboot·安全架构
CoderYanger4 天前
Java EE 进阶:9.MyBatis 操作数据库(进阶)(第二弹)
java·数据库·面试·职场和发展·java-ee·tomcat·mybatis
CoderYanger4 天前
Java EE 进阶:9.MyBatis 操作数据库(进阶)(第一弹)
java·数据库·spring·面试·java-ee·tomcat·mybatis
未秃头的程序猿4 天前
给新模块加了个 @MapperScan,启动崩了,最后发现跟 MapperScan 没关系
java·后端·mybatis
阿达和自己8 天前
springboot学习-springboot引入MyBatis-Plus
spring boot·学习·mybatis
2603_969694859 天前
【JavaWeb学习记录】MyBatis核心知识点与实战笔记:从入门到动态SQL
笔记·sql·学习·mysql·mybatis·javaweb·学习方法
wang_shu_mo_ran9 天前
MyBatis 从入门到进阶(一)
java·spring·mybatis
瑞雪兆丰年兮9 天前
JavaWeb-day14-SpringBootWeb原理篇
java·开发语言·spring boot·spring·bean·mybatis·javaweb