I'm having trouble tweaking a sql query that is returning duplicates. Its a simple query that returns data from a "wishlist". The structure of the wishlist is simple. 6 tables that feed into it:
Products - list of products, Images - list of images, Users - list of users, link_ProductsImages - links products to images, for one-to-many relationship (potentially), Wishlist - links users to products (and to images by proxy)
Here is some sample pretend data that sets up the following scenario. One user (100) has two products in his wishlist (1,2). ProductID 1 has one image associated with it (10), where ProductID 2 has three (11,12,13)
The goal is to have ONE LINE PER PRODUCT returned in the query. Because of the one-to-many of the products to images, Im getting duplicates returned on ProductID 2. Can someone help? I tried a few things I found on StackOverflow but I just can't seem to get the query ironed out.
Products ProductID - 1,2
Users UserID - 100
Images ImageID - 10, 11, 12, 13
Link_ProductsImages ProductID, ImageID - 1,10 2,11 2,12 2,13
Wishlist UserID, ProductID - 100, 1 100, 2
select w.WishlistID, p.*,
isnull(i.ImageFile, 'na.jpg') as ImageFile
from Products p
inner join Wishlist w on w.UserID = 4 and w.ProductID = p.ProductID
left outer join Link_ProductImage lpi on lpi.ProductID = p.ProductID
left outer join Images i on i.ImageID = lpi.ImageID