Load data infile default value not inserting in table

后端 未结 2 438
無奈伤痛
無奈伤痛 2021-01-21 21:09

I\'m importing a csv file using LOAD DATAINFILE

The csv columns are NAME,TYPE,STATUS

my table structure is

Name  : varchar
TYPE  : Varchar
Status: Ti         


        
2条回答
  •  一生所求
    2021-01-21 21:53

    When loading a file, MySQL expects that it has the same number of columns as the destination table, unless you specify otherwise, even if the missing column has a default value. So supply a column list to your LOAD statement, and a literal 1 for the value of STATUS:

    LOAD DATA INFILE '/var/www/names.csv' 
    INTO TABLE users 
      FIELDS TERMINATED BY ',' 
      ENCLOSED BY '"' 
      LINES TERMINATED BY '\n' 
      IGNORE 1 LINES 
      (`name`, `type`, 1)
    

    You can also do it with a SET clause:

    LOAD DATA INFILE '/var/www/names.csv' 
    INTO TABLE users 
      FIELDS TERMINATED BY ',' 
      ENCLOSED BY '"' 
      LINES TERMINATED BY '\n' 
      IGNORE 1 LINES 
      (`name`, `type`)
      SET `status` = 1
    

提交回复
热议问题