MySQL - Multiple set on LOAD DATA INFILE

前端 未结 5 500
没有蜡笔的小新
没有蜡笔的小新 2021-01-01 03:30

I\'ve a table_name like this:

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

and then I\'ve

相关标签:
5条回答
  • 2021-01-01 04:10
    LOAD DATA Local INFILE 'name.csv' INTO TABLE table1 
    FIELDS TERMINATED BY ','
    LINES TERMINATED BY '\n'
    IGNORE 1 LINES
        SET inserted_date=CURRENT_DATE(), inserted_by='me'
    
    0 讨论(0)
  • 2021-01-01 04:13

    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

    0 讨论(0)
  • 2021-01-01 04:15

    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'
    
    0 讨论(0)
  • 2021-01-01 04:17

    Insert bulk more than 7000000 record in 1 minutes in database(superfast query with calculation)

    mysqli_query($cons, '
        LOAD DATA LOCAL INFILE "'.$file.'"
        INTO TABLE tablename
        FIELDS TERMINATED by \',\'
        LINES TERMINATED BY \'\n\'
        IGNORE 1 LINES
        (isbn10,isbn13,price,discount,free_stock,report,report_date)
         SET RRP = IF(discount = 0.00,price-price * 45/100,IF(discount = 0.01,price,IF(discount != 0.00,price-price * discount/100,@RRP))),
             RRP_nl = RRP * 1.44 + 8,
             RRP_bl = RRP * 1.44 + 8,
             ID = NULL
        ')or die(mysqli_error());
    $affected = (int) (mysqli_affected_rows($cons))-1; 
    $log->lwrite('Inventory.CSV to database:'. $affected.' record inserted successfully.');
    

    RRP and RRP_nl and RRP_bl is not in csv but we are calculated that and after insert that.

    0 讨论(0)
  • 2021-01-01 04:17

    There is no way to do that using the load data command in mysql. The data has to be in your input file.

    If you are on Linux, it would be easy enough to add the additional field data to your input file using sed or similar tools.

    Another alternative would be to upload your file and then run a query to update the missing fields with the data you desire.

    You could also set up a trigger on the table to populate those fields when an insert happens, assuming you want to use the mysql username value.

    0 讨论(0)
提交回复
热议问题