I have 3 tables in my MySQL database :
- the Threads table (id, title)
- the Tags table (id, name)
- the pivot table ThreadTags (tagID, threadID)
Let's admit I already have the tag IDs in array $tagIDs, now I want to have all threads linked to ALL those tags. My current solution is something like that :
$stmt = 'SELECT id, title FROM Threads'."\n";
foreach($tagIDs as $id) {
$stmt .= 'INNER JOIN ThreadTags T1 ON (T1.threadID = Thread.id AND T1.tagID = '.$id.')'."\n";
}
And any number of tags I add another INNER JOIN
to this table.
Is there a better way ?
NB : please, no answer like "use a NoSQL database", I can't change that, thanks