Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySql copy value from one row to another

Tags:

mysql

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.

like image 567
Moak Avatar asked Sep 07 '09 05:09

Moak


1 Answers

i was looking for a solution to this and i came up on this post. however running the sql lead to a syntax error.

Refering to the mysql docs http://dev.mysql.com/doc/refman/5.0/en/update.html i noticed - at least in version 5 - mysql has a different syntax for this.

the general statement would be:

update table t1, table t2 
set t1.field1 = t2.field2, ..., t1.fieldN = t2.fieldN
where t1.someid = t2.someid and t1.fieldX = '...' and t2.fieldY = '...'
like image 117
memical Avatar answered Oct 06 '22 20:10

memical