Insert multiple data records using one mysql statement and multiple data records using one mysql statement

Source: Internet
Author: User

Insert multiple data records using one mysql statement and multiple data records using one mysql statement

This article mainly introduces how to insert multiple data records using an SQL statement in mysql, which is very efficient, but the principle is actually very simple. I hope it will be helpful to you.

 

Assume that there is A data table:

Id name title addtime

To insert n data entries:

1234567891011121314151617 $time= time();$data = array(  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;  ),   );

Previously, my idea was to construct multiple insert statements through data and call them cyclically. For example:

1234 $sql1 = "INSERT INTO `A`(`name`,`title`,`addtime`)VALUES ('name1','title1','".$time."')";$sql2 = "INSERT INTO `A`(`name`,`title`,`addtime`)VALUES ('name2','title2','".$time."')";......$sqlN = "INSERT INTO `A`(`name`,`title`,`addtime`)VALUES ('nameN','titleN','".$time."')";

After that, you can insert Multiple SQL insert statements at a time:

12345 $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 with the ",", you can use one SQL operation to insert multiple data. In the previous test, when the number of inserted data entries is 30, one insert is nearly nine times faster than multiple inserts of the same data. At the same time, because there is only one insert operation, it is similar to a transaction operation. If the insertion fails, all of them fail. If the insertion succeeds, all are successful, making data management more convenient. Therefore, if multiple data entries need to be inserted into the same table, try this method.

This is often the case when inserting multiple database records:

123456789 $b = 14; for($a=0;$a<100;$a++){    $sql = " INSERT INTO `roles` (`uid`,`rid`) VALUES (".$a.",".$b.")";    mysql_query($sql); }

However, this writing method is inefficient and requires multiple SQL statements to be executed. If you have used phpmyadmin to import data, you will find that the above statement can actually be written in this way.

12 INSERT INTO `roles` (`uid`,`rid`) VALUES    (534,14),(535,14),(536,14),(537,14),(539,14)

So the original code can be rewritten as follows:

123456789101112131415 $b = 14; 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);

Related Article

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.