我有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个或更多的diciplines,但即使两次相同也需要所有diciplines。我需要获得不同的学科价值观。我怎么能这样做?
答案 0 :(得分: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
答案 1 :(得分: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