后端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
相关推荐
化作星辰8 分钟前
java 给鉴权kafka2.7(sasl)发送消息权限异常处理
java·大数据·开发语言·kafka
无极小卒8 分钟前
如何在三维空间中生成任意方向的矩形内部点位坐标
开发语言·算法·c#
克里斯蒂亚诺更新12 分钟前
微信小程序 点击某个marker改变其大小
开发语言·前端·javascript
user_admin_god21 分钟前
企业级管理系统的站内信怎么轻量级优雅实现
java·大数据·数据库·spring boot
q***829121 分钟前
Spring Boot 3.3.4 升级导致 Logback 之前回滚策略配置不兼容问题解决
java·spring boot·logback
Kuo-Teng29 分钟前
LeetCode 206: Reverse Linked List
java·算法·leetcode·职场和发展
yaoxin52112330 分钟前
237. Java 集合 - 对 List 元素进行排序
java·windows
yaoxin52112335 分钟前
236. Java 集合 - 使用索引访问 List 元素
java·windows·list
humors2211 小时前
服务端开发案例(不定期更新)
java·数据库·后端·mysql·mybatis·excel
百***68041 小时前
JavaWeb项目打包、部署至Tomcat并启动的全程指南(图文详解)
java·tomcat