简体   繁体   中英

Multiple CSV to MySQL with Different Columns

I use the "load data infile" statement to load multiple .csv files in one table with a foreach loop. This works, only I have one problem. The columns in de .csv's are not the same. For example I have this:

CSV1:

id,name,ean,description

CSV2:

id,name,image,ean,description

And my MySQL table is:

id,name,ean,description

So because I want to import multiple csv's through a loop, I have a problem with CSV2, because of the image column. If possible, I want to match the name of the csv column with the name of the table column. So in this example, [image] is not imported. I can use a @ignore variable, but because every .csv is different, this doesn't work. I have this:

$sql = "
   LOAD DATA LOCAL INFILE '$value[path]'
       INTO TABLE db1.shoptest
       FIELDS TERMINATED BY '$value[seperator]'
       OPTIONALLY ENCLOSED BY '\"'
       LINES TERMINATED BY '\n'
     (id, name, ean, description)
    ;";

Is it possible to match the csv column names with the table column names and skip all other columns in the .csv?

Thanks so much for helping me out!

Kind regards, Mark

I think I understood the question. You wan't to get the column headers from the first row of the CSV, then build a string to use as the col_name_or_user_var argument of LOAD DATA and use @ignore variable assignments as needed.

id, name, state, ean, favorite_color, description

becomes...

(id, name, @bunk, ean, @bunk, description)

$whiteList = ['id', 'name', 'ean', 'description'];
$columnHeader = 'id, name, state, ean, favorite_color, description';
$columnHeader = explode(',', $s);
foreach ($columnHeader as $k => $v) {
    if (! in_array(trim($v), $whiteList)) $columnHeader[$k] = '@bunk';

}

echo implode(',', $columnHeaders);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM