tags:

views:

292

answers:

2

I am trying to import a large CSV file into a MySQL database. I have loaded the entire file into one flat table. i can select the data that needs to go into separate tables using select statements, my question is how do i copy the results of those select queries to different tables. i would prefer to do it completely in SQL and not have to worry about using a scripting language.

+2  A: 
INSERT
INTO    new_table_1
SELECT  *
FROM    existing_table
WHERE   condition_for_table_1;

INSERT
INTO    new_table_2
SELECT  *
FROM    existing_table
WHERE   condition_for_table_2;
Quassnoi
+3  A: 
insert into anothertable (list, of , column, names, to, give, values, for)
select list, of, column, names, of, compatible, column, types
from bigimportedtable
where possibly you want a predicate or maybe not;
tpdi
that works great i have duplicate primary keys in one of my tables now. is it possible in this query to concatenate two fields from the query result and add that to my table? take County_ID and Precienct_ID and make one field that would be like 121-603 and use that as my pk?
Eric
Well, yes you could. Just put that calculation in the select list: insert into foo(id) select concat(county_id), '-', precinct_id) from bar. BUT a composite ksy like this is a BAD idea, for several reasons. Are you by any chance making an electoral/voter file database?
tpdi