Question

First off, I apologize for the length. This is kind of complicated (at least for me).

Background on the database:

I have a products, variables, and prices table. "Products" are the main information regarding a product (description, title, etc). "Prices" have information about each price (price, cost, minimum qty required, shipping cost, etc), as some products can have more than one price (a 10" widget is a different price than a 12" widget, for instance). "Variables" are variations to the product that do not change the price, such as color, size, etc.

Initially (when I built this database about 7 years ago) I had the variable information stored in the first price in a list of prices for the same product in a pipe-delimited format (yes, I know, badbadbad). This worked in general, but we've always had a problem, though, where sometimes a variable wouldn't be consistent among all the prices.

For instance, a Widget (product) may be 10" or 12" and sell for $10 and $20 (prices) respectively. However, while the 10" widget may be available in blue and red (variables), the 12" widget is only available in red. We ameliorated this problem by adding a little parenthetical statement in the incongruent variable like "Red (10" ONLY)". This sort of works, but customers are not always that smart and a lot of time is devoted to fixing mistakes when a customer selects a 12" widget in red.

I have since been tasked with modernizing the database and have decided to put the variables in their own table and making them more dynamic and easier to match with certain prices, as well as keep a more dummy-proof inventory (you can't imagine the nightmares).

My first step was to write a stored procedure on my test db (for when I do the conversion) to process all the existing variables into a new variable table (and label table, but that's not really important, I don't think). I effectively parsed out the variables and listed them with the correct product id and the product id they were initially associated with in the variable table. However, I realized this is only a part of the problem, since I (at least for the initial transformation of the database) want each variable to be listed as being connected to each price for a given product.

To do this, I created another table, like so:

tblvariablesprices
variablepriceid | variableid | priceid | productid

which is a many-to-many with the variable table.

Problems:

My problem now is, I don't know how to create the rows. I can create a left join on my prices and variables tables to get (I think) all the necessary data, I just don't know how to go through it. My sql is (mysql 5.0):

SELECT p.priceid, p.productid, variableid, labelid 
FROM tblprices p 
LEFT JOIN tblvariables v ON p.priceid = v.priceid 
ORDER BY productid, priceid

This will get me every priceid and productid and any matching variable and label ids. This is good in certain instances, such as when I have something like:

priceid | productid | variableid | labelid
2       | 7         | 10         | 4
2       | 7         | 11         | 4
2       | 7         | 12         | 4
3       | 7         | (null)     | (null) --- another price for product

because now I know that I need to create a record for priceid 2 and variableids 10, 11, 12, and then also for priceid 3 for that product. However, I also get results from this dataset for products with no variables, products with one price and multiple variables, and products with multiple prices and no variables, for instance:

priceid | productid | variableid | labelid
2       | 7         | 10         | 4
2       | 7         | 11         | 4
2       | 7         | 12         | 4
3       | 7         | (null)     | (null)
4       | 8         | (null)     | (null) --- 1 price no variables
5       | 9         | 13         | 5      --- mult vars, 1 price
5       | 9         | 14         | 5
5       | 9         | 15         | 6
5       | 9         | 16         | 6
6       | 10        | (null)     | (null) --- mult price, no vars
7       | 10        | (null)     | (null)
8       | 10        | (null)     | (null)

Taking the above dataset, I want to add entries into my tblpricesvariables table like so:

variablepriceid | variableid | priceid | productid
1               | 10         | 2       | 7
2               | 11         | 2       | 7
3               | 12         | 2       | 7
4               | 10         | 3       | 7
5               | 11         | 3       | 7
6               | 12         | 3       | 7
7               | 13         | 5       | 9
8               | 14         | 5       | 9
9               | 15         | 5       | 9
10              | 16         | 5       | 9

I have thousands of records to process, so obviously doing this manually is not the answer. Can anyone at least point me in the correct direction, if not come up with a sproc that could handle this type of operation? I also would welcome any comments on how to better organize and/or structure this data.

Thank you so much for reading all this and helping me out.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top