0

Mysql LOAD command lets import data from csv files

LOAD DATA INFILE 'data.csv' INTO TABLE table_main
  FIELDS TERMINATED BY ',';

What if there are multiple relation tables

table_main
    id
    firstname
    lastname

table_type
    id
    table_main_id
    table_type_id

table_type
    id
    typename        

Is it possible to load a csv file with content like below with LOAD command

firstname, lastname, typename
john,doe,mytypename

3 Answers 3

1

No, it it not possible with LOAD DATA syntax to insert into multiple tables. A possible workaround is loading it into a temporary table & take it from there.

Sign up to request clarification or add additional context in comments.

Comments

0

No, IMHO. You would have to

  • first LOAD to temporary table, then
  • do the 3 INSERT statements and finally
  • DROP the temp table.

Comments

0

you can use an array that holds the tables names and iterate for each table name .

$tables = array("table1","table2","table3","tableN");

foreach ($tables as $table ) {

 try
     {
      $this->connect = new PDO("mysql:host=$db_host;dbname=$db_name",$db_user, $db_pass,array(PDO::MYSQL_ATTR_LOCAL_INFILE => true)); 

         $this->connect->exec("LOAD DATA INFILE  'data.csv' INTO TABLE 
                            ".$table_name." 
                  FIELDS TERMINATED BY ',' ";

   }
    catch(PDOException $e) 
    {  
        echo $e->getMessage(); 
    }

I hope that's solve your problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.