私のMySQLテーブルには3つの同一のテーブルがあります。
first_term_result、second_term_result、および third_term_result
これはその中の列です
exam_type_id | student_id | subject_id | mark |
またはダミーデータを使用した例
注:科目ごとに 3 つの異なる試験タイプ (CA1、CA2、CA3、および試験) があります。このような 3 つのテーブルがありますが、データは同じですが、データは異なります。 .
first_term_result:
exam_type_id | student_id | subject_id | mark |
1 | 6 | 7 | 12 |
2 | 6 | 7 | 9 |
3 | 6 | 7 | 13 |
4 | 6 | 7 | 45 |
1 | 4 | 7 | 7 |
2 | 4 | 7 | 5 |
3 | 4 | 7 | 10 |
4 | 4 | 7 | 34 |
second_term_result:
exam_type_id | student_id | subject_id | mark |
1 | 6 | 7 | 15 |
2 | 6 | 7 | 6 |
3 | 6 | 7 | 10 |
4 | 6 | 7 | 50 |
1 | 4 | 7 | 6 |
2 | 4 | 7 | 3 |
3 | 4 | 7 | 9 |
4 | 4 | 7 | 44 |
third_term_result:
exam_type_id | student_id | subject_id | mark |
1 | 6 | 7 | 17 |
2 | 6 | 7 | 8 |
3 | 6 | 7 | 15 |
4 | 6 | 7 | 67 |
1 | 4 | 7 | 12 |
2 | 4 | 7 | 8 |
3 | 4 | 7 | 12 |
4 | 4 | 7 | 50 |
今私が達成したいのはSUM()
、first_term_result.mark
second_term_result.mark
各third_term_result.mark
学生のWHERE
subject_id=7 グループを学生名で取得することです。
もう 1 つの非常に重要な問題は、first_term + second_term + third_term の各学生の総計を計算し、その学生と科目の総計を DESC で注文できるようにして、それに応じて配置できるようにすることです。 PHPで簡単に教えてください。
ありがとう
私には非常に複雑に思えますが、これを達成できるグルがここにいることを知っています。ロールアップを使用している場合でも注文できることをどこかで読みました。
以下は明らかに機能しない私のコードです。
SELECT CONCAT(s.fname,' ',s.mname,' ',s.lname) AS sname,
SUM(f.mark) AS first_total,
SUM(se.mark) AS second_total,
SUM(t.mark) AS third_total
SUM(f.first_total,second.total,third_total) as GT // just to show my intention
FROM students s, first_term_result f, second_term_result se, third_term_result t
WHERE s.studentID=f.student_id AND
s.studentID=se.student_id AND
s.studentID=t.student_id AND
f.subject_id=7 AND
se.subject_id=7 AND
t.subject_id=7
GROUP BY sname ORDER BY GT DESC