mysql data inline command - mysql

Is it possible to use "load data local infile" in mysql and set a column to a certain value...
So for example if my commannd is:
mysql_query('load data local infile "'.$filename.'" into table products fields terminated by "|" lines terminated by "\n" (column2, column3, column4)');
Then have a table like this
Column 1 | Column 2 | Column 3 | Column 4
can set column 1 equal to value I choose and have it done in this query? I am trying to have column 1 be equal to the filename it is importing...how would I set this into the sql query?

According to load data infile syntax you can do it like this:
mysql_query('load data local infile "'.$filename.'" into table products fields terminated by "|" lines terminated by "\n" (column2, column3, column4) SET column1=\'' . $filename . '\'');
From the link above: "The SET clause can be used to supply values not derived from the input file..."
(I'm not sure if something is messed up with single/double quotes...didn't try it).

Related

ignore first two characters on a column while importing csv to mysql

I am trying to import a csv file to mysql table, But I need to remove First two characters on particular column before importing to mysql.
This is my statment :
string strLoadData = "LOAD DATA LOCAL INFILE 'E:/park/Export.csv' INTO TABLE tickets FIELDS terminated by ',' ENCLOSED BY '\"' lines terminated by '\n' IGNORE 1 LINES (SiteId,DateTime,Serial,DeviceId,AgentAID,VehicleRegistration,CarPark,SpaceNumber,GpsAddress,VehicleType,VehicleMake,VehicleModel,VehicleColour,IssueReasonCode,IssueReason,NoticeLocation,Points,Notes)";
Column IssueReasoncode' has data like 'LU12' , But i need to remove the first 2 characters it should have only integers on it and not alpha numeric .
I need to remove 'LU' from that column.
Is it possible to write like this on left(IssueReasonCode +' '2). This column is varchar(45) and cant be changed now because of large data on it.
Thanks
LOAD DATA INFILE has the ability to perform a function on the data for each column as you read it in (q.v. here). In your case, if you wanted to remove the first two characters from the IssueReasonCode column, you could use:
RIGHT(IssueReasonCode, CHAR_LENGTH(IssueReasonCode) - 2)
to remove the first two characters. You specify such column mappings at the end of the LOAD DATA statement using SET. Your statement should look something like the following:
LOAD DATA LOCAL INFILE 'E:/park/Export.csv' INTO TABLE tickets
FIELDS terminated by ','
ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(SiteId, DateTime, Serial, DeviceId, AgentAID, VehicleRegistration, CarPark, SpaceNumber,
GpsAddress, VehicleType, VehicleMake, VehicleModel, VehicleColour, IssueReasonCode,
IssueReason, NoticeLocation, Points, Notes)
SET IssueReasonCode = RIGHT(IssueReasonCode, CHAR_LENGTH(IssueReasonCode) - 2)
Referencing this and quoting this example , you can try the below to see if it works
User variables in the SET clause can be used in several ways. The
following example uses the first input column directly for the value
of t1.column1, and assigns the second input column to a user variable
that is subjected to a division operation before being used for the
value of t1.column2:
LOAD DATA INFILE 'file.txt' INTO TABLE t1 (column1, #var1) SET
column2 = #var1/100;
string strLoadData = "LOAD DATA LOCAL INFILE 'E:/park/Export.csv' INTO TABLE tickets FIELDS terminated by ',' ENCLOSED BY '\"' lines terminated by '\n' IGNORE 1 LINES (SiteId,DateTime,Serial,DeviceId,AgentAID,VehicleRegistration,CarPark,SpaceNumber,GpsAddress,VehicleType,VehicleMake,VehicleModel,VehicleColour,#IRC,IssueReason,NoticeLocation,Points,Notes) SET IssueReasonCode = substr(#IRC,2) ;";

Load SQL infile with first column fixed width

I have a plain text file with data like this:
B01-CA This is first data
Z01 This is second data
A56-COL This is third data
So I want to insert these data in a table with 3 columns id, code, name.
I know the syntax:
load data local infile 'C:/codes.txt'
into table tbl_test
fields
terminated by ' '
enclosed by ' '
lines
terminated by '\n'
(code,name);
But how can I take first substring upto 8 charactrs in code column and rest line till '\n' to name column. id is auto increment. Any ideas will help.
I have had the same question.
I have done this:
load data local infile '<file>' into table <table_name> (#row) set code = trim(substr(#row, 0,7)), name = trim(substr(#row, 8))
it works for me

Import csv file with less columns than the table to which I'm importing?

I have exported a csv file with four columns in phpMyAdmin whith these options:
Columns separated with: ,
Columns enclosed with: "
Columns escaped with: "
Lines terminated with: AUTO
Now I want to import this file into a table with 17 columns!?
So only four columns will be populated and the others will stay empty.
You can spcify the column name while import data from file, and use user variable to discard an input value by assigning it to a user variable while import data in table .
AS PER MYSQL 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);
LOAD DATA INFILE 'filename.csv'
INTO TABLE sqlTable(column1, column2, column3, column4, #NotInsert2, #NotInsert2... till #NotInsert13);
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
So in Your case, Just specify four column name, and use any user variable instead of others column name so that total number of column in list will be equal to 17.
EDIT - Lets understand with Example:-
Consider we have to load file with following contents:
File-name: example.csv
col-2,col-3,col-4
a,2,3
b,4,5
c,6,7
Case :- When csv file have lesser number of columns than targeted table
table structure: tablename
col-1 col-2 col-3 col-4
Consider, col-1 is auto-increment and not provided in csv.
LOAD DATA INFILE ‘path/to/example.csv’
INTO TABLE tablename
FIELDS TERMINATED BY ‘,’
LINES
TERMINATED BY ‘\n’
IGNORE 1 LINES (col-2,col-3,col-4) set col-1=null;
Passing null value will make col-1 to take an auto-increment value.
Using SET you can assign values to those columns which were not available in csv and are not null.
For more information refer here
Try this:
LOAD DATA LOCAL INFILE 'XYZ.csv'
INTO TABLE tickets
FIELDS terminated by ','
ENCLOSED BY '\"' lines terminated by '\n' IGNORE 1 LINES
(column1, column2, column3, column4)(choose your data columns) ";

Import CSV into MySQL - Offset by 1 Column

I am importing my data from a csv file like this:
LOAD DATA LOCAL INFILE "c:\myfile.csv" INTO TABLE historic FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';
This ALMOST works perfect. My table has one different row, the first: An auto incremented id column. Otherwise the csv file and the MySQL table match perfectly. When I run the command above, it puts the first column from the csv into the id field. It want it to actually go into the second.
Is there a way I can modify the above statement to either specify the columns or just offset it by 1? (skipping the first column on import).
You can optionally name the columns to be populated by LOAD DATA, and simply omit your id column:
LOAD DATA LOCAL INFILE "c:\myfile.csv" INTO TABLE historic
FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n'
(column2, colum3, column4, column5, ...)
You can load your data specifing the order columns that you're going to use into your table:
LOAD DATA LOCAL FILE '/tmp/test.txt' INTO TABLE historic
(name_field1, name_field2, name_field3...);
Or if you prefer you can load it first to a temporary table and use a select into statement to laod it into your final table (it's slower).

mysql insert with auto-increment column

I am trying to insert data into a mysql table from a csv file. I am using the infile sql command, but I am having trouble because the first column of the table is an id that is set as an auto increment field. what do I have to set my first column value to in order to get this to work, or can I do it at all?
Thanks
Try using an empty field or a TAB character as a first column value in a file
(see comment "Posted by Mohamed Abdulla on August 17 2005 11:14am" on http://dev.mysql.com/doc/refman/5.0/en/loading-tables.html)
Another solution:
LOAD DATA INFILE 'DATA.txt' INTO TABLE your_table
FIELDS TERMINATED BY '^'
OPTIONALLY ENCLOSED BY '~'
(column2, column3, ... )
SET column1 = NULL