When I'm running my SQL query I got unwanted duplicates. In my query I will list (5) latest products added as the example below.
A product can have assigned multiple categories like this example:
- Product A, Category "men > clothes > pants"
- Product A, Category "women > clothes > pants"
- Product B, Category "men > clothes > shirts"
- Product B, Category "women > clothes > shirts"
Running my SQL query will give me the output:
- Product A
- Product A
- Product B
- Product B
What I want is just:
- Product A (Category "men > clothes > pants")
- Product B (Category "men > clothes > shirts")
Product table
ProductID
ProductName
Category join table
JoinID
ProductID
Category3ID
Category3 table
Category3ID
Category2ID
Cat3Name
Category2 table
Category2ID
Category1ID
Cat2Name
Category1 table
Category1ID
Cat1Name
SELECT TOP 5 * FROM Product P
INNER JOIN Product_Category_Join PCJ ON (P.ProductID = PCJ.ProductID)
INNER JOIN Category3 C3 ON (PCJ.Category3ID = C3.Category3ID)
INNER JOIN Category2 C2 ON (C3.Category2ID = C2.Category2ID)
INNER JOIN Category1 C1 ON (C2.Category1ID = C1.Category1ID)
ORDER BY P.Date DESC