I’ve tried very hard to have a normalized database and currently have three tables like so –
- content (c)
- content_tags (ct)
- tags (t)
What I’m trying to do here is select some rows and return them with all the tags that are associated with it. At the moment, it only returns one tag for some reason.
Here is the code I’m using:
SELECT *,
GROUP_CONCAT(t.tag) AS tags
FROM content AS c
LEFT JOIN category AS ca ON c.cid = ca.cid
LEFT JOIN contenttags AS ct ON c.smid = ct.smid
LEFT JOIN tags AS t ON ct.tid = t.tid
LEFT JOIN users AS u ON c.uid = u.uid
WHERE (t.tag IN ('tag1', 'tag2', 'tag3'))
GROUP BY c.smid
It returns something like this:
- cid
- content
- url
- tag (but only one instead of multiple)
Do you mean to find all contents and all their tags, where at least one of their tag is in
$tagArray?And those
LEFT JOINcan be probably turned into innerJOINs, too, without losing any results. Try it. Unlesscategory.cidorcategory.uidfields can haveNULLvalues. In that case, keep the relatedLEFT JOIN.