Execute an SQL statement update multiple records implementation ideas
What would you do if you wanted to update multiple rows of data, and the values of each field in each row were different? This article takes an example to explain how to achieve the situation as shown in the title, the needs of friends can understand the next
Typically, we use the following SQL statement to update field values:
UPDATE SET myfield=' value 'WHERE other_field=' Other_value ';
But what do you do if you want to update multiple rows of data, and each field value is different for each row of records? For example, my blog has three categories (free resources, tutorial guides, window displays), the information for these categories is stored in the database table categories , and the Display order field Display_order is set, with each category occupying a single row of records. If I want to rearrange the order of these categories, for example (Tutorial Guide, window display, free resources), then you need to update the Display_order field of the corresponding row of the categories table, which involves updating the multi-line record problem, At first you might think of a way to use loops to execute multiple UPDATE statements, like the following example of a PHP program:
foreach ($display _order$id$ordinal$sql=$ordinal$id"; mysql_query ($sql); }
There is nothing wrong with this method, and the code is easy to understand, but in the loop statement executes more than once SQL query, in the system optimization, we always want to reduce the number of database queries as much as possible to reduce resource consumption, while improving the system speed.
Fortunately, there are better solutions, the following list of two common scenarios is only a little more complex SQL statement, but only one query can be executed once, the syntax is as follows:
The first type: If–then statement combination
UPDATE SET Case when 1 Then ' value ' when 2 Then ' value ' when 3 Then ' value ' END WHERE inch (1,2,3
To get back to the example of our catalogue, we can use the following SQL statement:
UPDATE SET Case when 1 Then 3 when 2 Then 4 when 3 Then 5 END Case when 1 Then ' New Title 1 ' when 2 Then ' New Title 2 ' when 3 Then ' New Title 3 ' END WHERE inch (1,2,3
The above scheme greatly reduces the number of query operations of the database, greatly saves the system resources, but how to combine with our programming language? We still use the example of the directory just now, here is a sample PHP program:
$display _order = array ( 1 = 4 , 2 = 1 , 3 + 2 , 4 = 3 , 5 = 9 , 6 = 5 , 7 = 8 , 8 /span> = 9 ); $ids = implode ( ', ' , Array_keys ( $display _order )); $sql = "UPDATE categories SET display_order = case ID" ; foreach ( $display _order as $id = $ordinal ) { $sql . = sprintf ( "when%d and then%d" , $id , $ordinal ); //Splicing SQL statements } $sql . = "END WHERE ID in ($ids)" ; Echo $sql ; mysql_query ( $sql );
In this example, a total of 8 rows of data were updated, but only one database query was executed, and the time saved by the above example would be negligible compared to the 8 update statements executed in the loop. But think about the benefits you'll find when you need to update 10,0000 or more lines of records! The only thing to note is the length of the SQL statement, the length of the string that is supported by the program's running environment, the data I am currently getting: The SQL statement can still execute smoothly in PHP 1,000,960, I queried the PHP document and did not find the maximum length of the specified string.
The second type of insert
The insert syntax in MySQL has a condition of duplicate KEY update, which is used when it is necessary to determine whether a record is present or not, and the record is updated if there is no insert.
Based on the above scenario, the INSERT statement is still used for updating the record, but the field is updated when the primary key is limited to repeating. As follows:
INSERT into VALUES (1' Nick '[email protected] '), (4' Angel ',' [email protected] ' ), (7' Brank ',' [email protected] 'onKEYUPDATE name=values(name), email=values(email);
Note: on DUPLICATE KEY update is only a unique syntax for MySQL, not SQL standard syntax!
Article Source: Home of the script
Execute an SQL statement update multiple records implementation ideas