i have some problems here and i need help.
I have a table called posts, another table called tags and many-to-many relationship between them called item_tags
Here is the strucutre:
posts
- id
- title
- description
tags
- id
- name
- seo
item_tags
- id
- post_id
- tag_id
So let’s say now i want to build a query to match multiple tags by having their tag_id already. For an example let’s try to match all posts which has tag_id 11, tag_id 133 and tag_id 182. What i could do was selecting them with OR operator but these are not the results i want because what i want is to match all posts which has all mentioned tags only not just if contains some…
My query was:
SELECT * FROM item_tags
WHERE tag_id=’11’ OR tag_id=’133′ OR tag_id=’182′
Which is wrong…
Here is a screenshot of the table: https://i.imgur.com/X60HIM5.png
PS: I want to build a search based on multiple keywords (tags).
Thank you!
3
Answers
If you want all posts that have been tagged with all three tags, then you could use:
If you want posts tagged with any of those three tags use:
Important Note: I have not tested it!
You can group result by post_id and then save only those having all tags linked