I have 3 tables, message, subject and message_subject_rel
. The idea is to have messages that can relate to a lot of subjects and then do a cross subject search.
Lets say I have a message:
Id: 1, Message: This is a message
2 subjects:
Id:1, Subject: Math
Id:2, Subject: Science
And there's 2 message_subject_rel
entries that go:
Id: 1, message_id: 1, subject_id: 1
Id: 2, message_id: 1, subject_id: 2
If i wanted to search the messages that are related with math, I would do a simple join with the 3 tables and the where clause would be subject = "Math"
But what I don't know how to do, is search for the messages that are related with math AND Science. If i do a simple join i get tables with something like:
id message user_id created_at ip id message_id subject_id id subject
And if I do a where subject = "Math" and subject = "Science"
i wont get any results because each message will only have 1 subject related in each row, but duplicated rows for messages with more than 1 subject.
So, what do you recommend?