Skip to content

Instantly share code, notes, and snippets.

@halilim
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halilim/e7de9bd088409fa62526 to your computer and use it in GitHub Desktop.
Save halilim/e7de9bd088409fa62526 to your computer and use it in GitHub Desktop.
Copy districts from one db to another with differing province id's.

Local districts table:

`id`, `name`, `province_id`

Local provinces table:

`id`, `name`

Task: copy districts over from another DB with a different provinces table and different province id's.

  1. Add a temporary city_name to your local districts table.

  2. Run Copy_districts.sql.

  3. Review non-matching districts:

     SELECT * FROM districts
     LEFT JOIN provinces ON provinces.name LIKE districts.city_name;
    
  4. Remove the temporary city_name from districts.

INSERT districts
SELECT
NULL
, district_other.name
, district_other.province_other_id
, IFNULL(province_other.name, '')
FROM other_db.district_other
LEFT JOIN other_db.province_other ON province_other.id = district_other.province_other_id
;
UPDATE districts
LEFT JOIN province ON province.name LIKE districts.province_name
SET province_id = province.id
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment