我有以下问询,我很想找到更好的办法来做到这一点,因为我这样做的方式似乎并不正确。
EDIT
我没有具体指出,我只想回那些实际上与所有福利相匹配的广告!
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
我很想知道如何优化这些询问。