Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MySQL backfill missing data where records have one common field
- kim,,,55555,kim@domain.com
- kim,Longmont,CO,55555,kim@domain.com
- kim,,,,kim@domain.com
- kim,Longmont,CO,55555,kim@domain.com
- kim,Longmont,CO,55555,kim@domain.com
- kim,Longmont,CO,55555,kim@domain.com
- update t2,t1 set t2.first_name=
- case when t2.first_name=''
- then t1.first_name
- else t2.first_name end
- where t2.actor_id=t1.actor_id
- and t1.first_name!='';
- mysql> select * from t2 limit 10;
- +----------+------------+--------------+---------------------+
- | actor_id | first_name | last_name | last_update |
- +----------+------------+--------------+---------------------+
- | 1 | | GUINESS | 2012-06-29 08:36:22 |
- | 2 | | WAHLBERG | 2012-06-29 08:36:22 |
- | 3 | | CHASE | 2012-06-29 08:36:22 |
- | 4 | | DAVIS | 2012-06-29 08:36:22 |
- | 5 | | LOLLOBRIGIDA | 2012-06-29 08:36:22 |
- | 6 | | NICHOLSON | 2012-06-29 08:36:22 |
- | 7 | | MOSTEL | 2012-06-29 08:36:22 |
- | 8 | | JOHANSSON | 2012-06-29 08:36:22 |
- | 9 | | SWANK | 2012-06-29 08:36:22 |
- | 10 | CHRISTIAN | GABLE | 2006-02-15 04:34:33 |
- +----------+------------+--------------+---------------------+
- 10 rows in set (0.00 sec)
- mysql> select * from t1 order by actor_id limit 10;
- +----------+------------+--------------+---------------------+
- | actor_id | first_name | last_name | last_update |
- +----------+------------+--------------+---------------------+
- | 1 | PENELOPE | GUINESS | 2006-02-15 04:34:33 |
- | 1 | a | | 0000-00-00 00:00:00 |
- | 2 | NICK | WAHLBERG | 2006-02-15 04:34:33 |
- | 3 | ED | CHASE | 2006-02-15 04:34:33 |
- | 4 | JENNIFER | DAVIS | 2006-02-15 04:34:33 |
- | 5 | JOHNNY | LOLLOBRIGIDA | 2006-02-15 04:34:33 |
- | 6 | BETTE | NICHOLSON | 2006-02-15 04:34:33 |
- | 7 | GRACE | MOSTEL | 2006-02-15 04:34:33 |
- | 8 | MATTHEW | JOHANSSON | 2006-02-15 04:34:33 |
- | 9 | JOE | SWANK | 2006-02-15 04:34:33 |
- +----------+------------+--------------+---------------------+
- 10 rows in set (0.00 sec)
- mysql> update t2,t1 set t2.first_name=case when t2.first_name='' then t1.first_name else t2.first_name end where t2.actor_id=t1.actor_id and t1.first_name!='';
- Query OK, 9 rows affected (0.03 sec)
- Rows matched: 200 Changed: 9 Warnings: 0
- mysql> select * from t2 limit 10;
- +----------+------------+--------------+---------------------+
- | actor_id | first_name | last_name | last_update |
- +----------+------------+--------------+---------------------+
- | 1 | PENELOPE | GUINESS | 2012-06-29 08:37:34 |
- | 2 | NICK | WAHLBERG | 2012-06-29 08:37:34 |
- | 3 | ED | CHASE | 2012-06-29 08:37:34 |
- | 4 | JENNIFER | DAVIS | 2012-06-29 08:37:34 |
- | 5 | JOHNNY | LOLLOBRIGIDA | 2012-06-29 08:37:34 |
- | 6 | BETTE | NICHOLSON | 2012-06-29 08:37:34 |
- | 7 | GRACE | MOSTEL | 2012-06-29 08:37:34 |
- | 8 | MATTHEW | JOHANSSON | 2012-06-29 08:37:34 |
- | 9 | JOE | SWANK | 2012-06-29 08:37:34 |
- | 10 | CHRISTIAN | GABLE | 2006-02-15 04:34:33 |
- +----------+------------+--------------+---------------------+
- 10 rows in set (0.00 sec)
Add Comment
Please, Sign In to add comment