I have the following queries, and I would love to find a better way to do this as it doesn't seem right the way I am doing it...
EDIT
Sorry I did not specify that I only want to return adverts that actually match all amenities!
SELECT TOP 50 Advert.Id
FROM Advert
WHERE Id in(SELECT Advert_id FROM AdvertsToAmenities WHERE Amenity_id = 1 AND Advert_Id = Id)
AND Id in(SELECT Advert_id FROM AdvertsToAmenities WHERE Amenity_id = 3 AND Advert_Id = Id)
AND Id in(SELECT Advert_id FROM AdvertsToAmenities WHERE Amenity_id = 5 AND Advert_Id = Id)
-- OR --
SELECT TOP 50 Advert.Id
FROM Advert
JOIN AdvertsToAmenities a on Advert.Id = a.Advert_id
JOIN AdvertsToAmenities b on Advert.Id = b.Advert_id
JOIN AdvertsToAmenities c on Advert.Id = c.Advert_id
WHERE a.Amenity_id = 1
AND b.Amenity_id = 3
AND c.Amenity_id = 5
I would love to find out how to optimize these queries!