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。










