I have three tables: comics
, tags
and comicTags
.
The comics
table ID is connected through a foreign key with the comicTags
table while the tags
table ID is also connected to the comicTags
table through the tagID
.
comics table
+----+
| ID |
+----+
| 1 |
| 2 |
+----+
comicTags table
+---------+-------+
| comicID | tagID |
+---------+-------+
| 1 | 1 |
| 2 | 1 |
| 2 | 2 |
+---------+-------+
tags table
+----+-------+
| ID | tag |
+----+-------+
| 1 | tag1 |
| 2 | tag2 |
+----+-------+
What I'd like to achieve is, if I'm searching for tag1 AND tag2 I'd only like to get comic ID 2 as a result. Given is a string with the two tag names.
SELECT c.ID FROM `tags` `t`
LEFT JOIN comicTags AS ct
ON ct.tagID=t.ID
LEFT JOIN comics AS c
ON c.ID=ct.comicID
WHERE ((t.tag LIKE 'tag1')
OR (t.tag LIKE 'tag2'))
GROUP BY c.ID
With this statement I'm obviously getting comic ID 1 as well which I do not want. Changing from OR to AND doesn't work in the statement I've created.
Could you point me in the right direction on how to get only those comic IDs that match all tag IDs?