0

I've got legacy ecommerce database, which is awfully designed and I need to move all products from it to new solution. But I stuck with how product properties stored. So it basically has tens of tables named category_properties_<category_id> that have first column named product_id and then variable number of columns that contain properties_id.

I. e. there can be table category_properties_42 with records like

|<product1_id>|20|21|22|23|24|27|

and table category_properties_43 with records like

|<product2_id>|17|21|56|

Is it possible using MySQL to export this data in more reasonable format? Because variable number of columns and variable table names makes querying by product almost impossible. Thanks in advance.

  • I have a MySQL database with all those tables. And I want to convert them one normalized MySQL table. – evgeniuz Jan 13 '14 at 17:41
1

I would lean toward a single Category_Properties table with product_id, category_id, and optionally (if you need to retain the position of the property values in the original design) a sequence number of some sort. You are going to have to do some programming to convert this, but it can be done with some 'brute force' SQL if you are trying to avoid scripting something.

CREATE TABLE Category_Properties
( product_id
, category_id
, sequence_value  /* this is optional */
, property_id
);

INSERT INTO Category_Properties
SELECT * FROM (
  SELECT product_id, 43 AS cat, 1 AS seq, <first_property_col> AS pval FROM category_properties_43
   UNION
  SELECT product_id, 43, 2, <second_property_col> FROM category_properties_43
   UNION
  SELECT product_id, 43, 3, <third_property_col> FROM category_properties_43
 )
 WHERE pval IS NOT NULL
;

-- From your example this would be <first_property_col> = 17, then 21, then 56.

You can do this provided you know the max number of property columns for each category, the outer query will eliminate the missing values for products which don't use all the property columns. You can see why you might want to script this, rather than changing it for each new category.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.