后端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
相关推荐
C-SDN花园GGbond1 小时前
【探索数据结构与算法】插入排序:原理、实现与分析(图文详解)
c语言·开发语言·数据结构·排序算法
迷迭所归处2 小时前
C++ —— 关于vector
开发语言·c++·算法
架构文摘JGWZ3 小时前
Java 23 的12 个新特性!!
java·开发语言·学习
leon6253 小时前
优化算法(一)—遗传算法(Genetic Algorithm)附MATLAB程序
开发语言·算法·matlab
拾光师3 小时前
spring获取当前request
java·后端·spring
aPurpleBerry3 小时前
neo4j安装启动教程+对应的jdk配置
java·neo4j
锦亦之22334 小时前
QT+OSG+OSG-earth如何在窗口显示一个地球
开发语言·qt
我是苏苏4 小时前
Web开发:ABP框架2——入门级别的增删改查Demo
java·开发语言
姜太公钓鲸2334 小时前
c++ static(详解)
开发语言·c++
菜菜想进步4 小时前
内存管理(C++版)
c语言·开发语言·c++