3 tables joined with where clause
Posted by:
I. N.
Date: September 03, 2020 08:39AM
Hello,
I'm writing you 'cause I have a problem with this situation :
I have 3 tables like this
Content: id, title
1, A
2, B
3, C
Tag : id, tag
1, tagA
2, tagB
3, tagC
ContentTag : id, content_id, tag_id
1, 1, 1
2, 1, 2
3, 2, 1
4, 3, 3
I want to get a list of content with their tags. For this, I use this:
SELECT
c.id, c.title as title, CONCAT(GROUP_CONCAT(ta.tag)) as tags
FROM db_Content as c
LEFT JOIN db_ContentTag as ct ON ct.content_id = c.id
LEFT JOIN db_Tag as ta ON ta.id = ct.tag_id
GROUP BY 1
Everything works fine but now, I want to add a WHERE clause to search on tags name.
In my example above, I want to search tag containing "A" (LIKE '%A%') and get content 1 (A) and 2 (B).
I really don't know how to make it work.
Thanks is advance for your help.
Ibram