MySQL load csv into table by matching column titles

Caporegime
Joined
13 Jan 2010
Posts
32,750
Location
Llaneirwg
This is no doubt dead simple

I have a mysql table with column headers such as

Id, house, water

And a csv with line 1 headers such as

Id, waterlevel, house

How can I get the database table to match up using code..
Is it (along with the other load in file bits)

(@Id, @Water level, @House)

Set
Id = @Id
House = @House
Water = @waterlevel


I really would like to use the column id string (line 1 in csv) as the id rather than column1 as it means I avoid having to work out position in the csv to import

This is my first time populating a table.
 
Last edited:
Associate
Joined
7 Nov 2013
Posts
255
Location
Kent, England
You could either do the insert one at a time:

insert into mytable (Id, house, water) values (@Id, @House, @waterlevel)

And specify the parameters @Id, @House and @waterlevel or you could do it in batches:

insert into mytable (Id, house, water) values (@Id1, @house1, @waterlevel1), (@Id2, @house2, @waterlevel2)

And specify all the appropriate parameters.

How you specify the parameters will be dependent upon the language you are using.
 
Back
Top Bottom