0

我有 3 张桌子:

student (id, name)
discipline (id, name)
student_discipline (stud_id, disc_id, mark)

我需要从学生有 5 个或更多不同学科的学生中选择所有内容mark > 4

我有这个sql:

SELECT * FROM `student_discipline` a
LEFT JOIN `discipline` b ON a.disc_id = b.id
LEFT JOIN `student` c ON a.stud_id = c.id
WHERE a.mark > 4
GROUP BY c.id
HAVING COUNT(b.id) >= 5

此 sql 获取每个已提升 5 个或更多学科的学生,但即使两次相同,它也会获取所有学科。我需要获得不同的学科价值观。我怎样才能做到这一点?

4

2 回答 2

1

distinct在你的使用count()

SELECT * 
FROM `student_discipline` a
LEFT JOIN `discipline` b ON a.disc_id = b.id
LEFT JOIN `student` c ON a.stud_id = c.id
WHERE a.mark > 4
GROUP BY c.id
HAVING COUNT(distinct b.id) >= 5
于 2013-06-28T15:18:22.533 回答
0
SELECT DISTINCT  c.id, c.name
FROM `student_discipline` a
LEFT JOIN `discipline` b ON a.disc_id = b.id
LEFT JOIN `student` c ON a.stud_id = c.id
WHERE a.mark > 4
GROUP BY c.id
HAVING COUNT(distinct b.id) >= 5
于 2013-06-28T15:20:05.490 回答