Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Mashi
Aug 15, 2005

Just wanted you to know your dinner's cold and the children all agree you're a shitheel!
I have a properties table where I store data relating to a particular row in key/value pairs. It looks like this:

Table: person_properties
code:
  id, person_id, property_name, property_value
  1   45         name           joe
  2   45         hair_color     brown
  3   45         likes_tuna     1
  4   46         name           sally
  5   46         hair_color     blonde
  6   46         likes_tuna     0
So as you can see I'm just storing data pertaining to persons in a vertical rather than horizontal format, so that I can hold very different data without altering any tables.

My question is: What is the cleanest / most efficient way to retrieve this data as if it were stored the regular horizontal way? The way I'm doing it right now is:

code:
SELECT p.*,
       (SELECT property_value
        FROM   person_properties pp
        WHERE  pp.person_id = p.id
               AND property_name = "name") AS name,
       (SELECT property_value
        FROM   person_properties pp
        WHERE  pp.person_id = p.id
               AND property_name = "hair_color") AS hair_color,
       (SELECT property_value
        FROM   person_properties pp
        WHERE  pp.person_id = p.id
               AND property_name = "likes_tuna") AS likes_tuna
FROM   people p
WHERE  id = 45
I'd like to do this using a single join but I can't find legal way to dynamically set column names. So is there a way, or is this the best I am going to get?

Mashi fucked around with this message at 00:45 on Dec 12, 2007

Adbot
ADBOT LOVES YOU

Mashi
Aug 15, 2005

Just wanted you to know your dinner's cold and the children all agree you're a shitheel!
Wow and there I was really proud of myself for thinking of such a great table structure :saddowns:.

Thanks for the heads up, I'll reconsider it.

Mashi
Aug 15, 2005

Just wanted you to know your dinner's cold and the children all agree you're a shitheel!
Questions moved to own thread.

Mashi fucked around with this message at 18:48 on Dec 16, 2007

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply