Insert records into two table at once

﹥>﹥吖頭↗ 提交于 2020-01-05 01:51:10

问题


In my web page, there is a CSV import section. Users may import several thousands of records. I need to insert the csv details into the 2 tables.

The first table contains the basic information. And the second one contains the other additional information. So that i need to save the first table's Inserted ID into the second table.

For the above requirement, i wrote the 2 mysql statement. But it took more time to import. Here, is it possible to insert records into 2 table using single query?

Please advise.


回答1:


You cannot insert in two tables at once (because you need the id from the first one) but you can insert one-by-one for the first one and one single query for the second one as below:

$main_data = array('data1', 'data2', 'data3');
  $new_data = array();

    foreach($main_data as $data) {

      mysql_query("insert into table1 (data) values ('$data')");

      $new_id = mysql_insert_id();

      // Save the new id into an array + the data
      $new_data[$new_id] = $main;

    }

    $insert_into = array();

    // Create a new insert statement
    foreach($new_data as $new_key => $data) {
        $insert_into[] . "($new_key, '$data')"
    }

    $imploded_data = implode(',', $insert_into);

    if (count($insert_into) > 0) {

        // The result will be something like Insert into `table2` (id, value) values (1, 'data1'), (2, 'data2'),(3, 'data3');
        mysql_query("insert into `table2` (id, value) values $imploded_data");
    }



回答2:


Try this way ..

   $main_data = array('dino', 'babu', 'john');

    foreach($main_data as $main) {

      // Insert main to 1st table
      mysql_query("MY INSERT QUERY TO TABLE 1");

      // Get the last insert id
      $new_id = mysql_insert_id();

      // Insert the sub data to 2nd table using the insert id
      mysql_query("MY INSERT QUERY TO TABLE 2 USING $new_id ");
    }



回答3:


No, you can only insert in 1 table at a time.




回答4:


use transaction (http://dev.mysql.com/doc/refman/5.0/en/commit.html) to mantain the Atomicity of data.




回答5:


Since you have not shared your schema I will tell you story same way :)

I ll go this way

Step 1.old_max_import_id =select max(id) from csv_imports;

Step 2.Import data to csv_imports table

Step 3.Copy same data to csv_imports2, by runing following single query

insert into csv_imports2(fielda,fieldb,fieldc......)
   select fielda,fieldb,fieldc .....from csv_imports
      where id > old_max_import_id;

I think you can achieve this easily in yii. I am assuming you are importing csv data into table for Insertion. If you are updating by csv imports the stragegy will change significantly. In that case insert/update trigger on csv_imports table will help you.




回答6:


It took more time because you run a query for each line of CSV. Try to concatenate all SQL statement and exec all in one single time.

insert into table_one (First, Last) values ('Fred','Smith'),
  ('John','Smith'),
  ('Michael','Smith'),
  ('Robert','Smith');

insert into table_two (First, Last) values ('Fred','Smith'),
  ('John','Smith'),
  ('Michael','Smith'),
  ('Robert','Smith');


来源:https://stackoverflow.com/questions/19315976/insert-records-into-two-table-at-once

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