codeigniter 查询错误:子查询返回1行以上

ffvjumwh  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(117)

我有一个为我正在维护的应用程序编写的查询,它用于检查某个学生的结果,其他学生的结果也正常,但对于某些学生,它返回Query error: Subquery returns more than 1 row

$this->db->query("SELECT subjects.name AS subject, subjects.id as subject_id, marks.CA AS ca, marks.exam AS exam, marks.mark_obtained AS total, marks.percentages AS percentages, marks.student_id, marks.subject_id, marks.class_id, marks.section_id, marks.session, marks.term, marks.school_id, (SELECT grades.name FROM grades WHERE marks.mark_obtained >= grades.mark_from AND marks.mark_obtained <= grades.mark_upto AND grades.school_id=$school_id) AS grade, (SELECT grades.comment FROM grades WHERE marks.mark_obtained >= grades.mark_from AND marks.mark_obtained <= grades.mark_upto AND grades.school_id=$school_id) AS remark FROM users INNER JOIN students ON users.id = students.user_id INNER JOIN marks ON students.id = marks.student_id INNER JOIN subjects ON marks.subject_id = subjects.id WHERE marks.school_id = ? AND marks.student_id = ? AND marks.session = ? AND marks.term = ? AND marks.class_id = ? AND marks.section_id = ? ORDER BY marks.subject_id", array($school_id, $student_id, $session_id, $term, $class_id, $section_id))->result_array();
yyyllmsg

yyyllmsg1#

在查询中使用top 1,如下所示

SELECT subjects.name AS subject, subjects.id as subject_id, marks.CA AS ca, marks.exam AS exam, 
marks.mark_obtained AS total, marks.percentages AS percentages, marks.student_id,
marks.subject_id, marks.class_id, marks.section_id, marks.session, marks.term, marks.school_id, 
(SELECT grades.name FROM grades WHERE marks.mark_obtained >= grades.mark_from AND marks.mark_obtained <= grades.mark_upto AND grades.school_id=$school_id LIMIT 1) AS grade, 
(SELECT grades.comment FROM grades WHERE marks.mark_obtained >= grades.mark_from AND marks.mark_obtained <= grades.mark_upto AND grades.school_id=$school_id LIMIT 1) AS remark 
FROM users INNER JOIN students ON users.id = students.user_id 
INNER JOIN marks ON students.id = marks.student_id 
INNER JOIN subjects ON marks.subject_id = subjects.id 
WHERE marks.school_id = ? AND marks.student_id = ? AND marks.session = ? AND marks.term = ? AND marks.class_id = ? AND marks.section_id = ? 
ORDER BY marks.subject_id"

相关问题