I have two tables I want to merge:
city1(id, city, state, zip, lat, lon, county)
city2(id, city, state)
The tables contain overlapping city
and state
, but not primary keys. I can't figure out how to do this. UNION
won't work, nor INSERT IGNORE
due to the different column numbers and primary keys. So, I guess that leaves joins(left?), but I can't figure out the syntax to get it it excluding duplicates.
A NOTE
UNION
will still display duplicate city/state combinations. From what I've read, UNION
does prevent duplicates if the tables have different sizes.
Table Create Statements
Column and table names have changed
CREATE TABLE `cities` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`city` varchar(50) NOT NULL,
`state_code` char(2) NOT NULL,
PRIMARY KEY (`id`),
KEY `idx_state_code` (`state_code`)
) ENGINE=InnoDB AUTO_INCREMENT=29739 DEFAULT CHARSET=latin1
CREATE TABLE `cities_extended` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`city` varchar(50) NOT NULL,
`state_code` char(2) NOT NULL,
`zip` int(5) unsigned zerofill NOT NULL,
`latitude` double NOT NULL,
`longitude` double NOT NULL,
`county` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=41756 DEFAULT CHARSET=latin1
Best Answer
Proposed Solution #1
Explanation for Proposed Solution #1
Create Unique Indexes on (city,state_code) for both tables
Create Temp Table with rows from
cities
that's not incities_extended
Load the Temp Table into
cities
Discard the Temp Table
Proposed Solution #2
Explanation for Proposed Solution #2
INSERT IGNORE
only works with unique indexes and primary keys. In your case, a unique index oncity,state_code
was needed forcities_extended
.After making such an index, you can insert all columns except
id
.You could also include id like this
GIVE IT A TRY !!!