Import CSV file to MySQL | MySQL Import CSV

This below example shows you how to use the LOAD DATA INFILE statement to import CSV file into MySQL table.

The LOAD DATA INFILE statement allows you to read data from a text file and import the file’s data into a database table very fast.

Before importing the file, you need to complete the following steps :

1) Data Import table should be created
2) CSV Columns Should be match with the Database Table.
3) Access required to execute the query

LOAD DATA INFILE ‘<<CSV File Name>>’
INTO TABLE <<Table Name>>
FIELDS TERMINATED BY ‘,’
ENCLOSED BY ‘”‘
LINES TERMINATED BY ‘\n’
IGNORE 1 ROWS

Example :-

LOAD DATA INFILE ‘f:/LeadsDetails.csv’
INTO TABLE oldleads
FIELDS TERMINATED BY ‘,’
ENCLOSED BY ‘”‘
LINES TERMINATED BY ‘\n’
IGNORE 1 ROWS

Advertisements

MySql Import a record from another Database

How to import a record from another database table in mysql ?

Same Table Structure in Source and Destination Table

INSERT INTO <TABLENAME> SELECT * FROM <DESTINATION DATABASENAME>.<TABLENAME>

Transfer from Limited Fields

INSERT INTO <TABLENAME> (col1, col2) SELECT (col1, col2) FROM <DESTINATION DATABASENAME>.<TABLENAME>

Transfer from Limited Fields with Condition

INSERT INTO <TABLENAME> (col1, col2) SELECT (col1, col2) FROM <DESTINATION DATABASENAME>.<TABLENAME> where condition.field = condition.value

Mysql update a column with row no

Update a column with record row no in MySQL

  1. Execute the below query in Mysql to update an row no. in the specified column

 

My Sql Query Example 

SET @pos := 0
UPDATE userinfo SET fuserid = ( SELECT @pos := @pos + 1 ) ORDER BY updated_at DESC

  1. @pos -> Variable Name
  2. userinfo -> Table Name
  3. fuserid -> Column Name