Combining Multiple MySQL INSERT Statements in a Single Query
The question arises whether it is valid to combine multiple MySQL INSERT statements into a single query, as demonstrated in the following code:
<code class="php">$string1= "INSERT INTO....;"; $string1 .= "INSERT INTO....;"; $string1 .= "INSERT INTO....;"; mysql_query($string1) or die(mysql_error()); </code>
However, it is recommended to avoid this approach and instead employ a more efficient method of inserting multiple rows. The preferred approach is to use a single INSERT statement with multiple VALUES clauses, as shown in the following examples:
<code class="php">INSERT INTO a VALUES (1,23),(2,34),(4,33); INSERT INTO a VALUES (8,26),(6,29);</code>
This approach offers several advantages:
The above is the detailed content of Can Multiple MySQL INSERT Statements Be Combined in a Single Query?. For more information, please follow other related articles on the PHP Chinese website!