I have a list of products, and a list of related products.
I know I can retrieve the list of products, then retrieve the list of related products, using two SQL queries.
But I need to return the list of products, along with their related products, in the same query, in order.
To be clearer, once a product is found to have a related product, the related product should be immediately listed.
In other words, related products are immediately listed following a product. Both should be intermangled.
I need to avoid a SELECT Subquery for EACH product, as the list can be very long.
As an example, here are the tables :
TABLE_products
ProdId | ProdName |
---|---|
1 | Product Name 1 |
2 | Product Name 2 |
3 | Product Name 3 |
4 | Product Name 4 |
5 | Product Name 5 |
6 | Product Name 6 |
TABLE_related_products
ProdId_1 | ProdId_2 |
---|---|
1 | 5 |
6 | 1 |
Here are the results I need :
ProdId | ProdName |
---|---|
1 | Product Name 1 |
5 | Product Name 5 |
2 | Product Name 2 |
3 | Product Name 3 |
4 | Product Name 4 |
6 | Product Name 6 |
1 | Product Name 1 |
Thank you for your help, because I’m hitting my SQL knowledge on this one.
2
Answers
Here is how I achieved that. Using one single SQL call to get the intermangled was a dream (my dream). But in reality, it was not possible. The fact is that I forgot to mention that I need to list related products, but also products related to related products, until 3 times.
The @CharlesEF answer was VERY interesting : it returns the products as long as the associated products in secondary columns.
To make it simple, I decided to make one SQL query to get the products, and another second SQL query to get the associated products to those listed in the first SQL query.
Then, when I'm listing all the products, I'm just checking if its id is contained in the results of the second query. If so, I'm listing the associated product, thus inserting it in the list, then I continue to the next item.
I am not using PHP server-side, but this logic can be implemented in every language
That is the the way I chose to go. Probably not suitable for lists of 200,000 products, but efficient enough on moderate lists with approx 5000 products and 500 associated items, if fields are indexed.
This could be improved with expensive reasearch by SQL masters, but this would overflow the costs constraints associated to the project I'm working on.
A lot of thanks, as @CharlesEF ideas will surely be useful in the future for another project!
To elaborate:
Step 1, get list of all products:
Step 2, add related products:
This example has not been tested, it is code off the top of my head. The result returned will not be exactly what you want. You will need to use PHP code to get it as you want. You should get 2 listings for product 1 and 5. You can use ‘ORDER BY’ to sort on the DB side.
Post back if you have any questions/problems.
UPDATE:
You can get the results you want if you try this query:
Since this query is not tested you may have to play with a bit.
Post back if you have any questions/problems.
UPDATE 2:
Ok, after some sleep I see a problem with my previous update. This code is tested and returns the data you want. But, to put it in the format you want you need to use PHP code. I took into account that products can have more than 1 related item.
$newArray will contain the data, in the format of your sample data. Hope this helps.