Performing Bulk Inserts in a Single MySQL Query
Many scenarios require the insertion of multiple rows into a MySQL table. While it's possible to execute separate queries for each row, a more efficient approach is to perform a bulk insert using a single query.
Syntax:
MySQL provides a convenient syntax for inserting multiple rows in a single query:
INSERT INTO table (column1, column2, ...) VALUES (value11, value12, ...), (value21, value22, ...), ...;
Example:
Let's consider the scenario described in the question, where we need to insert a quantity of registered users into a table named "pxlot". The following query accomplishes this:
INSERT INTO pxlot (realname, email, address, phone, status, regtime, ip) VALUES ('$realname1', '$email1', '$address1', '$phone1', '0', '$dateTime1', '$ip1'), ('$realname2', '$email2', '$address2', '$phone2', '0', '$dateTime2', '$ip2'), ('$realname3', '$email3', '$address3', '$phone3', '0', '$dateTime3', '$ip3');
By replacing the placeholders with the actual values for each user, three new rows will be inserted into the "pxlot" table.
Benefits:
Bulk inserts offer several benefits:
Additional Notes:
The above is the detailed content of How Can I Perform Efficient Bulk Inserts into a MySQL Table Using a Single Query?. For more information, please follow other related articles on the PHP Chinese website!