I have two TABLEs from the US Census. One has 2.8 millions records in it, which is not only too large it contains data I simply don't need such as the 9-digit Zip Codes. I want to create a new table without the 9-digit Zip Codes, instead the 5-digit Zip Codes. I'm having trouble here, it might simply be the syntax, but I would greatly appreciate the help.
This query works to list all the rows:
SELECT DISTINCT ZIP5,
STATE_CODE,STATE,
COUNTY_CODE,COUNTY_NAME,
CBSA_CODE,CBSA_TITLE, CBSA_LSAD,
METRO_DIVISION_CODE, METRO_DIVISION_TITLE, METRO_DIVISION_LSAD,
CSA_CODE, CSA_TITLE, CSA_LSAD
from zip_code_to_cbsa;
I created the TABLE in PHP:
$sql = 'CREATE TABLE `zip5_code_to_cbsa` (
`id` INT unsigned NOT NULL AUTO_INCREMENT,
`ZIP5` VARCHAR(5) NOT NULL,
`STATE_CODE` VARCHAR(2) NOT NULL,
`STATE` VARCHAR(2) NOT NULL,
`COUNTY_CODE` VARCHAR(3) NOT NULL,
`COUNTY_NAME` VARCHAR(50) NOT NULL,
`CBSA_CODE` VARCHAR(5) NOT NULL,
`CBSA_TITLE` VARCHAR(50) NOT NULL,
`CBSA_LSAD` VARCHAR(50) NOT NULL,
`METRO_DIVISION_CODE` VARCHAR(5) NOT NULL,
`METRO_DIVISION_TITLE` VARCHAR(50) NOT NULL,
`METRO_DIVISION_LSAD` VARCHAR(50) NOT NULL,
`CSA_CODE` VARCHAR(3) NOT NULL,
`CSA_TITLE` VARCHAR(50) NOT NULL,
`CSA_LSAD` VARCHAR(50) NOT NULL,
primary key (id,ZIP5)
)';
Now to do the INSERT I attempted the following which gives an error:
INSERT into zip5_code_to_cbsa
SELECT DISTINCT ZIP5,
STATE_CODE,STATE,
COUNTY_CODE,COUNTY_NAME,
CBSA_CODE,CBSA_TITLE, CBSA_LSAD,
METRO_DIVISION_CODE, METRO_DIVISION_TITLE, METRO_DIVISION_LSAD,
CSA_CODE, CSA_TITLE, CSA_LSAD
from zip_code_to_cbsa;
mysql> INSERT into zip5_code_to_cbsa
-> SELECT DISTINCT ZIP5,
-> STATE_CODE,STATE,
-> COUNTY_CODE,COUNTY_NAME,
-> CBSA_CODE,CBSA_TITLE, CBSA_LSAD,
-> METRO_DIVISION_CODE, METRO_DIVISION_TITLE, METRO_DIVISION_LSAD,
-> CSA_CODE, CSA_TITLE, CSA_LSAD
-> from zip_code_to_cbsa;
**ERROR 1136 (21S01): Column count doesn't match value count at row 1**
mysql>
Thanks!
UPDATE:
This appears to be working for me now, unless someone can find a problem with it?
$sql = 'INSERT into zip5_code_to_cbsa (ZIP5,STATE_CODE,STATE,
COUNTY_CODE,COUNTY_NAME,
CBSA_CODE,CBSA_TITLE, CBSA_LSAD,
METRO_DIVISION_CODE, METRO_DIVISION_TITLE, METRO_DIVISION_LSAD,
CSA_CODE, CSA_TITLE, CSA_LSAD)
SELECT DISTINCT ZIP5 as ZIP5,
STATE_CODE,STATE,
COUNTY_CODE,COUNTY_NAME,
CBSA_CODE,CBSA_TITLE, CBSA_LSAD,
METRO_DIVISION_CODE, METRO_DIVISION_TITLE, METRO_DIVISION_LSAD,
CSA_CODE, CSA_TITLE, CSA_LSAD
from zip_code_to_cbsa;
';
The SELECT DISTINCT ZIP5 as ZIP5 allowed me to include the DISTINCT as part of this.