Guest User

Untitled

a guest
Sep 18th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. MySQL backfill missing data where records have one common field
  2. kim,,,55555,kim@domain.com
  3. kim,Longmont,CO,55555,kim@domain.com
  4. kim,,,,kim@domain.com
  5.  
  6. kim,Longmont,CO,55555,kim@domain.com
  7. kim,Longmont,CO,55555,kim@domain.com
  8. kim,Longmont,CO,55555,kim@domain.com
  9.  
  10. update t2,t1 set t2.first_name=
  11. case when t2.first_name=''
  12. then t1.first_name
  13. else t2.first_name end
  14. where t2.actor_id=t1.actor_id
  15. and t1.first_name!='';
  16.  
  17. mysql> select * from t2 limit 10;
  18. +----------+------------+--------------+---------------------+
  19. | actor_id | first_name | last_name | last_update |
  20. +----------+------------+--------------+---------------------+
  21. | 1 | | GUINESS | 2012-06-29 08:36:22 |
  22. | 2 | | WAHLBERG | 2012-06-29 08:36:22 |
  23. | 3 | | CHASE | 2012-06-29 08:36:22 |
  24. | 4 | | DAVIS | 2012-06-29 08:36:22 |
  25. | 5 | | LOLLOBRIGIDA | 2012-06-29 08:36:22 |
  26. | 6 | | NICHOLSON | 2012-06-29 08:36:22 |
  27. | 7 | | MOSTEL | 2012-06-29 08:36:22 |
  28. | 8 | | JOHANSSON | 2012-06-29 08:36:22 |
  29. | 9 | | SWANK | 2012-06-29 08:36:22 |
  30. | 10 | CHRISTIAN | GABLE | 2006-02-15 04:34:33 |
  31. +----------+------------+--------------+---------------------+
  32. 10 rows in set (0.00 sec)
  33.  
  34. mysql> select * from t1 order by actor_id limit 10;
  35. +----------+------------+--------------+---------------------+
  36. | actor_id | first_name | last_name | last_update |
  37. +----------+------------+--------------+---------------------+
  38. | 1 | PENELOPE | GUINESS | 2006-02-15 04:34:33 |
  39. | 1 | a | | 0000-00-00 00:00:00 |
  40. | 2 | NICK | WAHLBERG | 2006-02-15 04:34:33 |
  41. | 3 | ED | CHASE | 2006-02-15 04:34:33 |
  42. | 4 | JENNIFER | DAVIS | 2006-02-15 04:34:33 |
  43. | 5 | JOHNNY | LOLLOBRIGIDA | 2006-02-15 04:34:33 |
  44. | 6 | BETTE | NICHOLSON | 2006-02-15 04:34:33 |
  45. | 7 | GRACE | MOSTEL | 2006-02-15 04:34:33 |
  46. | 8 | MATTHEW | JOHANSSON | 2006-02-15 04:34:33 |
  47. | 9 | JOE | SWANK | 2006-02-15 04:34:33 |
  48. +----------+------------+--------------+---------------------+
  49. 10 rows in set (0.00 sec)
  50.  
  51. 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!='';
  52. Query OK, 9 rows affected (0.03 sec)
  53. Rows matched: 200 Changed: 9 Warnings: 0
  54.  
  55. mysql> select * from t2 limit 10;
  56. +----------+------------+--------------+---------------------+
  57. | actor_id | first_name | last_name | last_update |
  58. +----------+------------+--------------+---------------------+
  59. | 1 | PENELOPE | GUINESS | 2012-06-29 08:37:34 |
  60. | 2 | NICK | WAHLBERG | 2012-06-29 08:37:34 |
  61. | 3 | ED | CHASE | 2012-06-29 08:37:34 |
  62. | 4 | JENNIFER | DAVIS | 2012-06-29 08:37:34 |
  63. | 5 | JOHNNY | LOLLOBRIGIDA | 2012-06-29 08:37:34 |
  64. | 6 | BETTE | NICHOLSON | 2012-06-29 08:37:34 |
  65. | 7 | GRACE | MOSTEL | 2012-06-29 08:37:34 |
  66. | 8 | MATTHEW | JOHANSSON | 2012-06-29 08:37:34 |
  67. | 9 | JOE | SWANK | 2012-06-29 08:37:34 |
  68. | 10 | CHRISTIAN | GABLE | 2006-02-15 04:34:33 |
  69. +----------+------------+--------------+---------------------+
  70. 10 rows in set (0.00 sec)
Add Comment
Please, Sign In to add comment