MySQL statement: Batch update different values for multiple records [go]

Source: Internet
Author: User
Tags mysql update

MySQL statement: Batch update of different values for multiple records

MySQL UPDATE statement is simple, update a field of the data, generally write:

1 UPDATEmytable SET myfield = ‘value‘ WHEREother_field = ‘other_value‘;

If you update the same field with the same value,MySQL is also simple, modify where you can:

1 UPDATEmytable SET myfield = ‘value‘ WHERE other_field in(‘other_values‘);

Note here that ' other_values ' is a comma (,) delimited string, such as:

If you update more than one data for a different value, many people might write this:

1234 foreach   ( $display _ Order   as   $id   => $ordinal ) {      $sql   = "UPDATE categories SET Display_order = $ordinal WHERE id = $id"      mysql_query ( $sql }

That is, an update record that loops through one article. One record update once, so performance is poor, it is also easy to cause blocking.

So can a SQL statement implement batch update? MySQL does not provide a straightforward way to implement batch updates, but it can be accomplished with a little bit of skill.

1234567 UPDATEmytable    SETmyfield = CASEid        WHEN1 THEN‘value‘        WHEN2 THEN‘value‘        WHEN3 THEN‘value‘    ENDWHEREid IN (1,2,3)

This is a small trick to implement a batch update.
As an example:

1234567 UPDATEcategories    SETdisplay_order = CASEid        WHEN1 THEN3        WHEN2 THEN4        WHEN3 THEN5    ENDWHEREid IN (1,2,3)

This SQL means, update the Display_order field, if id=1 the value of Display_order is 3, if id=2 Display_order value is 4, if id=3 then Display_order value is 5.
That is, the conditional statements are written together.
The where section here does not affect the execution of the code, but it improves the efficiency of SQL execution. Make sure that the SQL statement executes only the number of rows that need to be modified, where only 3 of the data is updated, while the WHERE clause ensures that only 3 rows of data are executed.

If you update multiple values, you only need to modify them slightly:

010203040506070809101112 UPDATEcategories    SETdisplay_order = CASEid        WHEN 1 THEN3        WHEN2 THEN4        WHEN3 THEN5    END,    title = CASEid        WHEN1 THEN‘New Title 1‘        WHEN2 THEN‘New Title 2‘        WHEN3 THEN‘New Title 3‘    ENDWHEREid IN(1,2,3)

Here, a MySQL statement has been completed to update multiple records.
But to use in the business, you need to combine the service-side language, here in PHP, for example, constructs this MySQL statement:

0102030405060708091011121314151617 $display_order = array(    1 => 4,    2 => 1,    3 => 2,    4 => 3,    5 => 9,    6 => 5,    7 => 8,    8 => 9);$ids= implode(‘,‘, array_keys($display_order));$sql= "UPDATE categories SET display_order = CASE id ";foreach ($display_orderas$id=> $ordinal) {    $sql.= sprintf("WHEN %d THEN %d ", $id, $ordinal);}$sql.= "END WHERE id IN ($ids)";echo$sql;

In this example, there are 8 records to update. The code is easy to understand, have you learned it?

MySQL statement: Batch update different values for multiple records [go]

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.