tags:

views:

419

answers:

2

This should be pretty basic but i can't figure it out. Have a table 'values'

entity|attribute|value

frank - shirt - red 
frank - hat   - green
sam   - shirt - blue
sam   - hat   - orange

how do i set all hats color's to be the same as the shirt color, of the same person to give frank a red hat and sam a blue hat.

+1  A: 

subselects may be...

set hat color of one random shirt of some person

update values set value = (select value from values where entity = v.entity and attribute = 'shirt' limit 1) from values v where v.attribute = 'hat'

Wrote in browser, so not tested, but you can view an idea.

Alexey Sviridov
+1  A: 

Give this a try:

UPDATE table 
SET value = res.value
FROM table INNER JOIN table res
ON table.entity = res.entity
WHERE (table.attribute = 'hat') AND (res.attribute = 'shirt')

This is assuming an entity can have at most 1 shirt.

Roee Adler
looks good, however I get a sql syntax error: http://pastebin.com/m508afd12
Moak