MYSQL import data from csv using LOAD DATA INFILE

前端 未结 11 1535
感动是毒
感动是毒 2020-11-22 09:12

I am importing some data of 20000 rows from a CSV file into Mysql.

Columns in the CSV are in a different order than MySQL table\'s columns. How to automatically assi

11条回答
  •  耶瑟儿~
    2020-11-22 09:45

    Before importing the file, you must need to prepare the following:

    • A database table to which the data from the file will be imported.
    • A CSV file with data that matches with the number of columns of the table and the type of data in each column.
    • The account, which connects to the MySQL database server, has FILE and INSERT privileges.

    Suppose we have following table :

    CREATE TABLE USING FOLLOWING QUERY :

    CREATE TABLE IF NOT EXISTS `survey` (
      `projectId` bigint(20) NOT NULL,
      `surveyId` bigint(20) NOT NULL,
      `views` bigint(20) NOT NULL,
      `dateTime` datetime NOT NULL
    );
    

    YOUR CSV FILE MUST BE PROPERLY FORMATTED FOR EXAMPLE SEE FOLLOWING ATTACHED IMAGE :

    If every thing is fine.. Please execute following query to LOAD DATA FROM CSV FILE :

    NOTE : Please add absolute path of your CSV file

    LOAD DATA INFILE '/var/www/csv/data.csv' 
    INTO TABLE survey 
    FIELDS TERMINATED BY ',' 
    ENCLOSED BY '"'
    LINES TERMINATED BY '\r\n'
    IGNORE 1 LINES;
    

    If everything has done. you have exported data from CSV to table successfully

提交回复
热议问题