Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL - Multiple set on LOAD DATA INFILE

Tags:

mysql

csv

load

I've a table_name like this:

No | Name | Inserted_Date | Inserted_By
=====================================

and then I've file name.csv like this

no,name
1,jhon
2,alex
3,steve

I want to load these file table_name using syntax like this:

LOAD DATA INFILE 'name.csv' INTO TABLE table1
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 LINES
???

the question is, what should I put on ??? so I can store data like this:

No | Name  | Inserted_Date | Inserted_By
=====================================
1  | jhon  | sysdate()     | me
2  | ales  | sysdate()     | me
3  | steve | sysdate()     | me
like image 216
Praditha Avatar asked Sep 21 '12 12:09

Praditha


People also ask

What is Local_infile mysql?

The local_infile system variable controls server-side LOCAL capability. Depending on the local_infile setting, the server refuses or permits local data loading by clients that request local data loading. By default, local_infile is disabled. (This is a change from previous versions of MySQL.)

Which is more efficient load data insert?

LOAD DATA (all forms) is more efficient than INSERT because it loads rows in bulk.

How do you load data into a mysql table?

mysql> LOAD DATA LOCAL INFILE '/path/pet. txt' INTO TABLE pet; If you created the file on Windows with an editor that uses \r\n as a line terminator, you should use this statement instead: mysql> LOAD DATA LOCAL INFILE '/path/pet.

How do I change local Infile in mysql?

Open my. ini file with a text editor. Look for the headers [client] , [mysql] under the CLIENT section & [mysqld] under the SERVER section. Save the file and restart MySQL80 service under the Windows local services.


2 Answers

I do not understand if columns inserted_date and inserted_by already exists in your table. If no than you can add them before runing LOAD DATA INFILE:

LOAD DATA INFILE 'name.csv' INTO TABLE table1
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(@no, @name)
set
  no = @no,
  name = @name,
  inserted_date = now(),
  inserted_by = 'me'
like image 69
ravnur Avatar answered Sep 19 '22 15:09

ravnur


something like this will do it:

LOAD DATA INFILE 'name.csv' INTO TABLE table1 
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 LINES
    SET inserted_date=CURRENT_DATE(), inserted_by='me'

Take a look at the manual: http://dev.mysql.com/doc/refman/5.1/en/load-data.html

like image 29
Nin Avatar answered Sep 19 '22 15:09

Nin