后端JOIN、LEFT JOIN、RIGHT JOIN的理解

复制代码
        SELECT f_exam_record.*, f_exam_paper.PaperName, f_exam_paper.PaperTime,exam_class.classname FROM f_exam_record
            JOIN f_exam_paper ON f_exam_record.PaperId = f_exam_paper.PaperId
            LEFT JOIN exam_class on f_exam_record.classId=exam_class.classid  where f_exam_record.ExamUserNumber=#{ExamUserNumber};
  • FROM f_exam_record:

    • 查询的主表是 f_exam_record,即查询的基础表。
  • JOIN f_exam_paper ON f_exam_record.PaperId = f_exam_paper.PaperId:

    • JOIN : 默认是 INNER JOIN,它将 f_exam_record 表与 f_exam_paper 表连接起来,连接条件是 f_exam_record.PaperId = f_exam_paper.PaperId
    • 效果 : 查询结果中只包含 f_exam_record 表和 f_exam_paper 表中 PaperId 匹配的记录。即 f_exam_record 中每条记录都会与 f_exam_paper 中的相应记录配对。
  • LEFT JOIN exam_class ON f_exam_record.classId = exam_class.classid:

    • LEFT JOIN : 将 f_exam_record 表与 exam_class 表连接,连接条件是 f_exam_record.classId = exam_class.classid
    • 效果 : 查询结果中包含所有来自 f_exam_record 表的记录,以及 exam_class 表中与之匹配的记录。如果 exam_class 表中没有与 f_exam_record 匹配的记录,则对应的 exam_class 字段(如 classname)将显示为 NULL
  • WHERE f_exam_record.ExamUserNumber = #{ExamUserNumber}:

    • 过滤条件 : 只返回 f_exam_record 表中 ExamUserNumber 等于指定值的记录。

LEFT JOIN和RIGHT JOIN的理解如下

  • LEFT JOIN : 返回左表的所有记录和右表中匹配的记录。右表中没有匹配的记录则填充为 NULL
  • RIGHT JOIN : 返回右表的所有记录和左表中匹配的记录。左表中没有匹配的记录则填充为 NULL
相关推荐
老马啸西风20 小时前
敏感词性能提升14倍优化全过程 v0.29.0
java
天天代码码天天20 小时前
C++ opencv RTSP小工具 RTSP流播放、每一帧保存
开发语言·c++·opencv
Cloud-Future20 小时前
Spring MVC 处理请求的流程
java·spring·mvc
小扳20 小时前
SpringBootWeb 篇-深入了解 ThreadLocal 存在内存泄漏问题
java·开发语言·spring boot·面试
CryptoPP20 小时前
跨境金融数据对接实践:印度NSE/BSE股票行情API集成指南
开发语言·后端·金融
lxsy20 小时前
spring-ai-alibaba-deepresearch 学习(十三)——ResearcherNode
java·源码分析·deepresearch·ai-alibaba
ShineWinsu21 小时前
对于单链表相关经典算法题:206. 反转链表及876. 链表的中间结点的解析
java·c语言·数据结构·学习·算法·链表·力扣
迦蓝叶21 小时前
JAiRouter 配置文件重构纪实 ——基于单一职责原则的模块化拆分与内聚性提升
java·网关·ai·重构·openai·prometheus·单一职责原则
ST.J21 小时前
系统架构思考20241204
java·笔记·系统架构
要做朋鱼燕21 小时前
【C++】 list 容器模拟实现解析
开发语言·c++·笔记·职场和发展·list