MySQL - Multiple set on LOAD DATA INFILE

本小妞迷上赌 提交于 2019-12-18 17:25:38

问题


I've a table_name like this:

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

and then I've file name.csv like this

no,name
1,jhon
2,alex
3,steve

I want to load these file table_name using syntax like this:

LOAD DATA INFILE 'name.csv' INTO TABLE table1
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 LINES
???

the question is, what should I put on ??? so I can store data like this:

No | Name  | Inserted_Date | Inserted_By
=====================================
1  | jhon  | sysdate()     | me
2  | ales  | sysdate()     | me
3  | steve | sysdate()     | me

回答1:


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'



回答2:


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




回答3:


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.




回答4:


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.




回答5:


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'


来源:https://stackoverflow.com/questions/12530971/mysql-multiple-set-on-load-data-infile

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!