0

I have 3 tables:

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

I need to select everything from students where a student has 5 or more different disciplines with mark > 4

I have this 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

This sql gets every student that has promovated 5 or more diciplines but it takes all diciplines even if it's the same twice. I need to get distinct disciplines values. How can I do that?

1
  • Are you saying you have duplicate ID numbers? Commented Jun 28, 2013 at 15:20

2 Answers 2

1

Use distinct in your 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
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I knew about the DISTINCT method but didn't know how to use it
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.