后端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
相关推荐
coderxiaohan23 分钟前
【C++】多态
开发语言·c++
gfdhy33 分钟前
【c++】哈希算法深度解析:实现、核心作用与工业级应用
c语言·开发语言·c++·算法·密码学·哈希算法·哈希
闲人编程42 分钟前
Python的导入系统:模块查找、加载和缓存机制
java·python·缓存·加载器·codecapsule·查找器
Eiceblue1 小时前
通过 C# 将 HTML 转换为 RTF 富文本格式
开发语言·c#·html
故渊ZY1 小时前
Java 代理模式:从原理到实战的全方位解析
java·开发语言·架构
匿者 衍1 小时前
POI读取 excel 嵌入式图片(支持wps 和 office)
java·excel
leon_zeng01 小时前
Qt Modern OpenGL 入门:从零开始绘制彩色图形
开发语言·qt·opengl
会飞的胖达喵1 小时前
Qt CMake 项目构建配置详解
开发语言·qt
ceclar1231 小时前
C++范围操作(2)
开发语言·c++
一个尚在学习的计算机小白1 小时前
java集合
java·开发语言