MySQL Forums
Forum List  »  Newbie

Re: GROUP_CONCAT question
Posted by: laptop alias
Date: March 17, 2010 09:31AM

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!

Options: ReplyQuote


Subject
Written By
Posted
March 17, 2010 09:25AM
Re: GROUP_CONCAT question
March 17, 2010 09:31AM
March 17, 2010 10:08AM
March 17, 2010 10:49AM
March 17, 2010 07:51PM
March 18, 2010 01:05PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.