Insert multiple data with a Mysql statement _mysql

Source: Internet
Author: User

If there is a data table A:

ID Name Title Addtime

If you need to insert an n-piece of data:

$time = time ();
$data = Array (
   ' name ' => ' name1 ', ' title ' => ' Title1 ', ' addtime ' => $time;
  ),
  Array (
   ' name ' => ' name2 ', ' title ' => ' Title2 ', ' addtime ' => $time;
  ),
  Array (
   ' name ' => ' Name3 ', ' Title ' => ' Title3 ', ' addtime ' => $time;
  Array (
   ' name ' => ' Namen ', ' title ' => ' Titlen ', ' addtime ' => $time;
  )
  
;

Before my idea would be, to construct multiple INSERT statements through data, loop the call. Such as:

$sql 1 = "INSERT into ' A ' (' name ', ' title ', ' Addtime ') VALUES (' name1 ', ' title1 ', '", $time.) ";
$sql 2 = "INSERT into ' A ' (' name ', ' title ', ' Addtime ') VALUES (' name2 ', ' title2 ', '", $time.) ";
......
$sqlN = "INSERT into ' A ' (' name ', ' title ', ' Addtime ') VALUES (' Namen ', ' titlen ', '", $time.) ";

The SQL INSERT statement was later found to be inserted at one time:

$sql = "INSERT into ' A ' (' name ', ' title ', ' Addtime ') VALUES (' name1 ', ' title1 ', '", $time. "),";
$sql. = "(' name2 ', ' title2 ', '", $time. "),";
$sql. = "(' Name3 ', ' title3 ', '", $time. "),";
.....
$sql. = "(' Namen ', ' titlen ', '", $time.) ";

By separating multiple data through the "," number, one SQL operation can be used to resolve the insertion of multiple data, which is nearly 9 times times faster than inserting the same data at one time in the case of inserting the data bar number 30. At the same time, because the insert operation is only once, it is similar to a transaction operation, the insertion fails all, and the success of the insert succeeds, making the data management more convenient. So, if you have more than one piece of data that needs to be inserted into the same table, try this way.

When you insert more than one database record earlier, this is often the case:

$b =;

For ($a =0 $a <100; $a + +) {

   $sql = INSERT into ' roles ' (' uid ', ' rid ') VALUES (". $a.", ". $b.") ";

   mysql_query ($sql);



However, this writing is inefficient and requires multiple execution of SQL statements. If you've used phpMyAdmin to import data, you'll actually find that the above statement can actually be written

   INSERT into ' roles ' (' uid ', ' rid ') VALUES
       (534,14), (535,14), (536,14), (537,14), (539,14)

So the original code can be rewritten like this.

 $b =;

For ($a =0 $a <100; $a + +) {

  if ($a ==0)

     $sql = INSERT into ' roles ' (' uid ', ' rid ') VALUES (". $a.", ". $b.") ";

  else

    $sql. = ", (". $a. ",". $b. ")";

}

mysql_query ($sql);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.