patternsqlMinor
Pure (My)SQL transformations
Viewed 0 times
sqlpuretransformations
Problem
I'm trying to promote the usage of a database versioning system (http://code.google.com/p/flyway/) on the project I'm currently working on - and to that end when we make changes to the database, we're propagating those changes through a series of deltas. It's still very early doors at the moment, so we've had the luxury of being able to make "destructive" deltas and re-populate the DB post-delta.
We are nearing the point where we need to be able to restructure the DB with production data, and so I want to be able to ideally move data around the database within MySQL alone.
For example - I want to take the column contents of TABLE_1:the_value, create the column on TABLE_2 and then copy the values from TABLE_1 to TABLE_2 WHERE id_org and id_club match
I've used the FOR EACH ROW construct with Triggers before, but cannot find documentation on the MySQL site that suggests or demonstrates how to use that to iterate over a result set in order to copy it to the target table. That may be more of a reflection of my inability to locate the appropriate information..
Can anyone offer me any pointers on this matter?
We are nearing the point where we need to be able to restructure the DB with production data, and so I want to be able to ideally move data around the database within MySQL alone.
For example - I want to take the column contents of TABLE_1:the_value, create the column on TABLE_2 and then copy the values from TABLE_1 to TABLE_2 WHERE id_org and id_club match
TABLE 1
--------------------------------
| id_org | id_club | the_value |
--------------------------------
| 213 | 213 | 1 |
--------------------------------
| 214 | 213 | 2 |
--------------------------------
| 212 | 210 | 5 |
--------------------------------
-- intervening DDL that adds "the_value" to table 2
-- 2nd row on table 1 not copied across due to absence of matching id's
TABLE 2
--------------------------------------------
| id_org | id_club | foo | the_value |
--------------------------------------------
| 213 | 213 | bar | 1 |
--------------------------------------------
| 212 | 210 | bar | 5 |
--------------------------------------------I've used the FOR EACH ROW construct with Triggers before, but cannot find documentation on the MySQL site that suggests or demonstrates how to use that to iterate over a result set in order to copy it to the target table. That may be more of a reflection of my inability to locate the appropriate information..
Can anyone offer me any pointers on this matter?
Solution
Assuming TABLE 2 already exists with id_org, id_club, and foo, just create the 'the_value' column and issue an UPDATE call using joins
See the UPDATE manual
See the UPDATE manual
UPDATE table1 t1, table2 t2 SET t2.the_value=t1.the_value
WHERE t1.id_org = t2.id_org AND t1.id_club = t2.id_clubCode Snippets
UPDATE table1 t1, table2 t2 SET t2.the_value=t1.the_value
WHERE t1.id_org = t2.id_org AND t1.id_club = t2.id_clubContext
StackExchange Database Administrators Q#2603, answer score: 4
Revisions (0)
No revisions yet.