Import multiple CSV fields into one MySQL field
<p>I have a table like this</p>

<pre><code>mytable(`id` int, 'number1' varchar(11), 'number2' varchar(1200))
</code></pre>

<p>Also I have cvs-like file</p>

<pre><code>111111111,222222222,333333333,44444444,,,
222222222,333333333,
111111111,555555555,666666666,
</code></pre>

<p>They are separated by ","(or something else)
The csv have 100 colunms.</p>

<p>I would like to combine the second column to 100rd loumn into mysql "number2",
the first column into mysql "number1".</p>

<p>like this:</p>

<pre><code>id number1    number 2
1  111111111  222222222,333333333,44444444
2  222222222  333333333
3  111111111  555555555,666666666
</code></pre>

<p>So can I use LOAD DATA INFILE to load the file into the table? How can I do this..? or have other method?</p>

<p>thanks.</p>

<p>you can use the function <code>file()</code> to open the cvs file ;</p>

<p>use the function <code>explode(',', $each_line)</code> to seperate each line with <code>,</code>,</p>

<p>I did not understand your demand clearly,I think simple code could like this:</p>

<pre><code>$file_items = file('file.cvs');
foreach($file_items as $item) {
    $item_arr = explode(',', $item);
    // combine $item_arr[0]... $item_arr[99] ,you get $number1, $number2;
    $sql_insert = "INSERT INTO mytable (number1, number2) VALUES(number1, $number2)";
}
</code></pre>

python mysql csv