This is not too complicated. Suppose you have the following tables:
- Customers , CustomerID primary key
- Products , primary key ProductID
- Orders , primary key OrderID, foreign key CustomerID
- OrderItems , primary key OrderItemID, foreign keys OrderID, ProductID
To find the products you are looking for, you need to find the set of customers who have purchased this product identifier:
SELECT CustomerID FROM (Customers INNER JOIN (Orders INNER JOIN OrderItems)) WHERE OrderItem.ProductID = <your product id here>
Then you need to get other products that these customers bought:
SELECT ProductID FROM (Customers INNER JOIN (Orders INNER JOIN OrderItems)) WHERE (Customer = <given customer ID>) AND (ProductID <> <your product id>)
Then select some of the best products and you will go racing.
Note. I am a numerical guy. Guru-gurus will be able to do this in 1 request! :)
Drew hall
source share