0

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.

4

2 回答 2

2

You need to specify the column names explicitly if you are inserting to selected columns only,

INSERT INTO tableName (colA, colB)
SELECT colA, colB
FROM tableName

what you are doing now is incorrect because the server assumes that you are inserting a value to all columns but you have supplied less than the number columns, that's why it generate an error

Column count doesn't match value count
于 2012-10-14T16:08:34.683 回答
0

Why not do it in one go?

CREATE TABLE new_table AS 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;

ALTER TABLE new_table ADD id INT NOT NULL AUTO_INCREMENT PRIMARY KEY;
于 2012-10-14T16:09:57.500 回答