CREATE TABLE order_info
( order_id INT NOT NULL
, product_id INT NOT NULL
, option_attribute VARCHAR(20)
, option_value VARCHAR(20)
, PRIMARY KEY(order_id,product_id,option_attribute,option_value)
);
INSERT INTO order_info VALUES
(1,10,'Colour','Red'),
(1,10,'Size','Large'),
(1,10,'Sleeve','Short'),
(2,10,'Colour','Blue'),
(2,10,'Size','Small'),
(2,10,'Sleeve','Long');
SELECT order_id `Order`
, product_id Product
, MAX(CASE WHEN option_attribute = 'Colour' THEN option_value END) Colour
, MAX(CASE WHEN option_attribute = 'Size' THEN option_value END) Size
, MAX(CASE WHEN option_attribute = 'Sleeve' THEN option_value END) Sleeve
FROM order_info
GROUP
BY order_id
, product_id;
+-------+---------+--------+-------+--------+
| Order | Product | Colour | Size | Sleeve |
+-------+---------+--------+-------+--------+
| 1 | 10 | Red | Large | Short |
| 2 | 10 | Blue | Small | Long |
+-------+---------+--------+-------+--------+
Note that this kind of thing is normally handled at the application level (e.g. with a bit of PHP) - oh, and the EAV model is sometimes discouraged!