I'm struggling for hours with something that seems so easy but I cant make it to work or find something similar in Google.
I have 2 tables
images
and
tags
They have a relation of Many to Many so I have another pivot table named image_tag
What Im trying to achive is select all images that has tag_id=4 and tag_id=1
My first attemp was something like this:
SELECT * from images as a INNER JOIN
image_tag as b on a.id=b.image_id
WHERE b.tag_id=4 and b.tag_id=1
Of couse this gave me 0 result as you cant use AND condition directly to pivot.
After that I tried this:
SELECT * FROM images as a
INNER JOIN image_tag as b on a.id=b.image_id
WHERE b.tag_id IN (1,4)
This returns all the images that either has tag_id=1
or tag_id=4
tried also Inner joining the same pivot table but cant make it to work no matter what
EDIT: Adding the sql fiddle. http://sqlfiddle.com/#!9/1726b0/1 the result should be images with ids 4,5,6,7