I'm trying to create a SQL query that selects users based on their common interests...
I only want to select the users that have all 3 interests in common, not every user that has one or more of these interests... This is what i have come up with so far - just lots of subqueries...
students.student_id in (select student_id from student_interests where student_id in (select student_id from student_interests where student_id in (select student_id from student_interests where interest_id = (select interest_id from interests where interest = 'xbox')) and interest_id = (select interest_id from interests where interest = 'programming')) and interest_id = (select interest_id from interests where interest = 'neowin'))
The student_interests table is just a pivot table containing the student_id and interest_id
and the interests table contains the interest_id and the interest string.
Is this the best way to do it or have is there a better way of doing it.
Question
Guest
I'm trying to create a SQL query that selects users based on their common interests...
I only want to select the users that have all 3 interests in common, not every user that has one or more of these interests... This is what i have come up with so far - just lots of subqueries...
The student_interests table is just a pivot table containing the student_id and interest_id
and the interests table contains the interest_id and the interest string.
Is this the best way to do it or have is there a better way of doing it.
thanks guys...
Link to comment
Share on other sites
3 answers to this question
Recommended Posts