后端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
相关推荐
夏天的味道٥3 小时前
使用 Java 执行 SQL 语句和存储过程
java·开发语言·sql
IT、木易4 小时前
大白话JavaScript实现一个函数,将字符串中的每个单词首字母大写。
开发语言·前端·javascript·ecmascript
冰糖码奇朵4 小时前
大数据表高效导入导出解决方案,mysql数据库LOAD DATA命令和INTO OUTFILE命令详解
java·数据库·sql·mysql
好教员好4 小时前
【Spring】整合【SpringMVC】
java·spring
Mr.NickJJ5 小时前
JavaScript系列06-深入理解 JavaScript 事件系统:从原生事件到 React 合成事件
开发语言·javascript·react.js
浪九天5 小时前
Java直通车系列13【Spring MVC】(Spring MVC常用注解)
java·后端·spring
Archer1946 小时前
C语言——链表
c语言·开发语言·链表
My Li.6 小时前
c++的介绍
开发语言·c++
堕落年代6 小时前
Maven匹配机制和仓库库设置
java·maven
功德+n6 小时前
Maven 使用指南:基础 + 进阶 + 高级用法
java·开发语言·maven