Mysql execute pivot table query
I have created sql fiddle http://sqlfiddle.com/#!9/0586e8/1
I have table called products,tags,product_tags.I want to retrieve all product tags which has searched tags
For example if i search for Mi tag
then it should return all tags from the product which has tag Mi Tag
You can use exists
to find the match:
SELECT DISTINCT t.tag_name FROM product_tags pt JOIN tags t ON pt.tag_id = t.id WHERE EXISTS (SELECT 1 FROM product_tags pt2 JOIN tags t2 ON pt2.tag_id = t2.id WHERE pt2.product_id = pt.product_id AND t2.tag_name = 'Mi' );
This is saying: Get me all tags where the corresponding product has a tag of "MI".
Here is a SQL Fiddle.