All we need is an easy explanation of the problem, so here it is.
I have table with students – name, id
and table with grades – id, grade value, discipline.
Each students has few grades. I want to select only the students with grade = 2 and grade = 6 in the same time.
SELECT NAME FROM STUDENTS, GRADES WHERE STUDENTDS.ID = GRADES.ID AND GRADE = 2 AND GRADE = 6
The logical condition can’t be valid there. Which is the function that I need?
How to solve :
I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.
SELECT NAME FROM STUDENTS s WHERE EXISTS (SELECT 1 FROM GRADES WHERE ID = s.ID AND GRADE = 2) AND EXISTS (SELECT 1 FROM GRADES WHERE ID = s.ID AND GRADE = 6)
or if you don’t have multiple records for some grades for particular users:
SELECT NAME FROM STUDENTS JOIN GRADES ON STUDENTS.ID = GRADES.ID WHERE GRADE = 2 OR GRADE = 6 GROUP BY ID, NAME HAVING COUNT(1) = 2
Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂