4

I have a csv file having 5 columns and i want to load only one out of that, is there a way i can do it. I want to use LOAD statement of MySQL 5.5 Any pointers are welcomed, help would be much appreciated

7

From the documentation:

You can also discard an input value by assigning it to a user variable and not assigning the variable to a table column:

LOAD DATA INFILE 'file.txt'
  INTO TABLE t1
  (column1, @dummy, column2, @dummy, column3);
  • I have gone through the documentation, but i have a csv file say - 46,94,43,12 now i want to discard the third column, LOAD DATA INFILE 'file.txt' INTO TABLE t1("WHAT SHOULD I WRITTE HERE?? THE COLUMN NAMES??)... Thanks for your time Phil.. – Hitesh Chouhan May 16 '13 at 4:54
  • LOAD DATA INFILE 'file.txt' INTO TABLE t1 (column1, column2, @discard3, column4); – Philᵀᴹ May 16 '13 at 5:02
0

you can save the csv field value to a variable and assign the variable to the table field.

load data infile 'filelocation../file.csv' into 
    table myTable columns terminated by ',' optionally enclosed by '"'
    escaped by '"' lines terminated by '\n'(@value1,@value2,@discardValue,@value4) 
    set f1=@value1 ,f2=@value2,f3=@value4;
  • Seems identical to Phil's suggestion, only this longer syntax makes you introduce more variables. – Andriy M Aug 31 '16 at 6:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.