我在学生和分数之间有一个简单的关系:
学生:
+------------+-------------+
| idStudent | NameStudent |
+------------+-------------+
| 1 | Student A |
| 2 | Student B |
| 3 | Student C |
+------------+-------------+
标记:
+----------+------+-----------+
| idMarks | mark | idStudent |
+----------+------+-----------+
| 1 | A | 1 |
| 2 | A | 1 |
| 3 | A | 1 |
| 4 | A | 2 |
| 5 | A | 2 |
| 6 | C | 3 |
| 7 | A | 3 |
+----------+------+-----------+
我只想让学生在他们通过的每门考试中都获得“A”
SELECT *, COUNT(mark)
FROM student S
INNER JOIN marks M ON S.idStudent = M.idStudent
WHERE M.mark = "A"
GROUP BY S.idStudent
我试过了,但是当我试图比较他们通过的考试数量和他们获得 A 的数量时,我遇到了僵局...
我也试过子查询,但没用
最佳答案
你可以使用HAVING
:
SELECT S.idStudent
FROM student S
INNER JOIN marks M ON S.idStudent = M.idStudent
GROUP BY S.idStudent
HAVING COUNT(mark)=SUM(mark='A');-- compare all marks with marks that are only 'A'
-- if equal then every mark is 'A'
关于mysql - 只有成绩最好的学生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52485440/