带计数操作的SQL连接查询

时间:2015-06-04 09:10:14

标签: mysql

大家好我有一个包含下表的数据库

学生表:

+------------+-----------+----------+------------+
| Studeni_ID | Firstname | Lastname | Contact    |
+------------+-----------+----------+------------+
|          1 | Deen      | Nute     |   85124512 |
|          2 | Helen     | Jude     | 2147483647 |
|          3 | Howard    | Mindy    |    8965123 |
+------------+-----------+----------+------------+

first_exam表:

+------------+--------------+-------+------+------+
| Studeni_ID | Subject_Name | Score | Year | Term |
+------------+--------------+-------+------+------+
|          1 | English      |    54 | 2015 |    1 |
|          1 | Math         |    48 | 2014 |    2 |
|          1 | History      |    85 | 2015 |    1 |
|          2 | English      |    59 | 2015 |    1 |
|          2 | Math         |    65 | 2015 |    1 |
|          3 | English      |    59 | 2015 |    1 |
|          2 | science      |    41 | 2015 |    1 |
+------------+--------------+-------+------+------+

last_exam table:

+------------+--------------+-------+------+------+
| Studeni_ID | Subject_Name | Score | Year | Term |
+------------+--------------+-------+------+------+
|          1 | English      |    75 | 2015 |    1 |
|          2 | English      |    86 | 2015 |    1 |
|          3 | History      |    72 | 2015 |    1 |
+------------+--------------+-------+------+------+

我正在努力计算2015年和第1学期所有学生考试的数量,所以这里是我尝试的查询及其输出:

SELECT First_Exam.Studeni_ID, student.Firstname, student.Lastname, 
COUNT(First_Exam.Subject_Name)AS FirstE,
COUNT(Last_Exam.Subject_Name)AS LAST_E 
FROM student,First_Exam,Last_Exam  
WHERE student.Studeni_ID=First_Exam.Studeni_ID=Last_Exam.Studeni_ID 
AND First_Exam.Year=2015 AND First_Exam.Term=1 
AND Last_Exam.Year=2015 AND Last_Exam.Term=1 
GROUP BY First_Exam.Studeni_ID;

输出:

+------------+-----------+----------+--------+--------+
| Studeni_ID | Firstname | Lastname | FirstE | LAST_E |
+------------+-----------+----------+--------+--------+
|          1 | Deen      | Nute     |      2 |      2 |
|          2 | Helen     | Jude     |      3 |      3 |
|          3 | Howard    | Mindy    |      1 |      1 |
+------------+-----------+----------+--------+--------+

SELECT First_Exam.Studeni_ID, student.Firstname, student.Lastname, COUNT(First_Exam.Subject_Name)AS First_E,COUNT(Last_Exam.Subject_Name)AS Last_E FROM First_Exam 
INNER JOIN student 
ON student.Studeni_ID=First_Exam.Studeni_ID 
INNER JOIN Last_Exam 
ON First_Exam.Studeni_ID=Last_Exam.Studeni_ID 
WHERE First_Exam.Year=2015 AND First_Exam.Term=1 AND Last_Exam.Year=2015 AND Last_Exam.Term=1 GROUP BY First_Exam.Studeni_ID ;

输出:

+------------+-----------+----------+---------+--------+
| Studeni_ID | Firstname | Lastname | First_E | Last_E |
+------------+-----------+----------+---------+--------+
|          1 | Deen      | Nute     |       2 |      2 |
|          2 | Helen     | Jude     |       3 |      3 |
|          3 | Howard    | Mindy    |       1 |      1 |
+------------+-----------+----------+---------+--------+

他们的输出都有错误,任何人都可以帮我解决这个问题....

1 个答案:

答案 0 :(得分:0)

我加入学生表和第一个考试表,并且只在年份和学期如你所说的那样对行进行求和,这样才能确保所有来自学生表的学生都会来,即使他们没有做过任何考试。 然后我会用同样的格式结合上次考试。

为什么有2个表具有相同的结构,您可以将它们存储在同一个表中。

然后我最后用学生ID将两个表组的联合值相加。

以下查询应该有效

select Studeni_ID, sum(cnt) FROM (
select Studeni_ID , SUM(case when year = 2015  then 1 else 0 end) cnt FROM students left join first_exam on students.Studeni_ID  = first_exam.Studeni_ID group by students.Studeni_ID
union
select Studeni_ID , SUM(case when year = 2015  then 1 else 0 end) cnt FROM students left join last_exam on students.Studeni_ID  = last_exam.Studeni_ID group by students.Studeni_ID
) group by Studeni_ID