Many to Many Table SQL Selection Problem

redrumak47

New Member
I have two tables that are related, which, for the purpose of this question, will be called \[code\]posts\[/code\] and \[code\]tags\[/code\]. \[code\]posts\[/code\] contains various postings, such as those found on a community forum system. \[code\]tags\[/code\] contains a unique set of tags, that are added when the server encounters a new tag that is not already in the system. There is only one entry per tag.\[code\]posts\[/code\] can have multiple tags, and \[code\]tags\[/code\] can have more than one post that is a reference to it. To handle these references back and forth, I have created a table to sit in between these two tables, called \[code\]posttags\[/code\]. \[code\]posttags\[/code\] contains a reference to the \[code\]tag\[/code\] id and the \[code\]post\[/code\] id. This is how the many to many relationship is maintained.Now, on to the problem. I need to be able to select posts based on a tag. This is a simple join when there is only one tag to search for, but I am at a loss as to how to handle multiple tags. For instance, I need to be able to search the database and get results that have ALL of the tags that are in a list (e.g., "php, mysql, sql"), without using SQL inside of a loop or any other low performance options.I am not sure how to do this. Can anyone point me in the correct direction?Thanks!
 
Back
Top