SQL 查询返回考试结果中的所有科目而不是指定科目

问题描述 投票:0回答:1

我有一个查询应该返回学生在考试中特定科目的分数。但是,查询返回所有科目的分数,而不仅仅是指定科目的分数。

database result

这是我正在使用的查询:

SELECT DISTINCT
    es.student_id AS exam_student_id,
    s.firstname AS student_firstname,
    s.lastname AS student_lastname,
    sub.name AS subject_name,
    eca.get_ca1 AS ca1_marks,
    eca.get_ca2 AS ca2_marks,
    eca.get_ca3 AS ca3_marks,
    eca.get_ca4 AS ca4_marks,
    eca.get_ca5 AS ca5_marks,
    eca.get_ca6 AS ca6_marks,
    eca.get_exam AS exam_marks,
    eca.get_tot_score AS total_score,
    eca.rem1 AS remark1,
    eca.rem2 AS remark2
FROM
    exam_group_class_batch_exam_students es
JOIN
    exam_group_class_batch_exams egcbe ON es.exam_group_class_batch_exam_id = egcbe.id
JOIN
    exam_group_class_batch_exam_subjects egcbes ON egcbe.id = egcbes.exam_group_class_batch_exams_id
JOIN
    subjects sub ON egcbes.subject_id = sub.id
JOIN
    exam_group_exam_results eca ON es.id = eca.exam_group_class_batch_exam_student_id
JOIN
    students s ON es.student_id = s.id
JOIN
    student_session ss ON s.id = ss.student_id
JOIN
    classes c ON ss.class_id = c.id
JOIN
    sections sec ON ss.section_id = sec.id
JOIN
    exam_groups eg ON egcbe.exam_group_id = eg.id
WHERE
    ss.session_id = 19 
    AND c.id = 12 
    AND sec.id = 6
    AND sub.id = 43  -- Filtering by the specific subject ID
    AND eg.id = 4 
    AND egcbe.id = 80
ORDER BY
    s.lastname, s.firstname, sub.name;

问题: 即使我在 WHERE 子句中指定了主题 ID (sub.id = 43),查询仍然返回所有主题的分数,而不仅仅是 ID 为 43 的主题。

我尝试过的: 确保 JOIN 条件将科目 ID 与考试结果正确关联。 添加了 DISTINCT 子句以避免重复记录,但问题仍然存在。 预期行为: 查询应该只返回 sub.id = 43 的主题的结果。

实际行为: 查询返回所有主题的结果,而不仅仅是指定主题。

问题: 可能是什么原因导致此问题?如何修改我的查询,使其仅返回指定主题的结果?

mysql phpmyadmin
1个回答
0
投票

您的加入 exam_group_class_batch_exam_students(别名为 es)和 exam_group_class_batch_exam_subjects(别名为 egcbes)过于广泛,并且加入了太多结果。尝试像这样指定您的加入:

SELECT DISTINCT
    es.student_id AS exam_student_id,
    s.firstname AS student_firstname,
    s.lastname AS student_lastname,
    sub.name AS subject_name,
    eca.get_ca1 AS ca1_marks,
    eca.get_ca2 AS ca2_marks,
    eca.get_ca3 AS ca3_marks,
    eca.get_ca4 AS ca4_marks,
    eca.get_ca5 AS ca5_marks,
    eca.get_ca6 AS ca6_marks,
    eca.get_exam AS exam_marks,
    eca.get_tot_score AS total_score,
    eca.rem1 AS remark1,
    eca.rem2 AS remark2
FROM
    exam_group_class_batch_exam_students es
JOIN
    exam_group_class_batch_exams egcbe ON es.exam_group_class_batch_exam_id = egcbe.id
JOIN
    exam_group_class_batch_exam_subjects egcbes 
    ON egcbe.id = egcbes.exam_group_class_batch_exams_id 
    AND es.exam_group_class_batch_exam_subject_id = egcbes.id  -- Ensure the join only matches the specific subject
JOIN
    subjects sub ON egcbes.subject_id = sub.id
JOIN
    exam_group_exam_results eca ON es.id = eca.exam_group_class_batch_exam_student_id
JOIN
    students s ON es.student_id = s.id
JOIN
    student_session ss ON s.id = ss.student_id
JOIN
    classes c ON ss.class_id = c.id
JOIN
    sections sec ON ss.section_id = sec.id
JOIN
    exam_groups eg ON egcbe.exam_group_id = eg.id
WHERE
    ss.session_id = 19 
    AND c.id = 12 
    AND sec.id = 6
    AND sub.id = 43  -- Filtering by the specific subject ID
    AND eg.id = 4 
    AND egcbe.id = 80
ORDER BY
    s.lastname, s.firstname, sub.name;
© www.soinside.com 2019 - 2024. All rights reserved.