MySQL Forums
Forum List  »  Newbie

Re: Query problem
Posted by: laptop alias
Date: January 29, 2010 03:40AM

This is a daft setup. People have gone insane trying to use EAV models before. Mix that with alcohol and you've got a recipe for disaster. That said, I'll show you the principle and leave you to it.

Consider this example:
SELECT * FROM  eav_hell;
+--------------------+-----------+-------+
| entity             | attribute | value |
+--------------------+-----------+-------+
| Merlot             | Colour    | Red   |
| Merlot             | Year      | 2009  |
| Merlot             | Size      | 75cl  |
| Cabernet Sauvignon | Colour    | Red   |
| Cabernet Sauvignon | Year      | 2008  |
| Cabernet Sauvignon | Size      | 75cl  |
| Pinot Grigio       | Colour    | White |
| Pinot Grigio       | Year      | 2008  |
| Pinot Grigio       | Size      | 75cl  |
+--------------------+-----------+-------+

SELECT entity Wine
     , MAX(CASE WHEN attribute = 'Colour' THEN value END) Colour
     , MAX(CASE WHEN attribute = 'Year' THEN value END) Year
     , MAX(CASE WHEN attribute = 'Size' THEN value END) Size
  FROM eav_hell
 GROUP
    BY Wine;
+--------------------+--------+------+------+
| Wine               | Colour | Year | Size |
+--------------------+--------+------+------+
| Cabernet Sauvignon | Red    | 2008 | 75cl |
| Merlot             | Red    | 2009 | 75cl |
| Pinot Grigio       | White  | 2008 | 75cl |
+--------------------+--------+------+------+

Pity the poor, abandoned data types.

Options: ReplyQuote


Subject
Written By
Posted
January 28, 2010 09:13AM
January 28, 2010 09:15AM
January 28, 2010 09:30AM
January 28, 2010 09:36AM
January 28, 2010 01:05PM
January 28, 2010 01:14PM
January 28, 2010 05:07PM
January 28, 2010 05:52PM
January 29, 2010 03:08AM
Re: Query problem
January 29, 2010 03:40AM
January 29, 2010 06:11AM


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.