Update Multiple Rows With Different Values and a Single SQL Query

Sometimes you may need to update multiple rows of data in a table. This is no problem a lot of the time, as the fields to update may all need to be changed to the same value, in which case you might run a query like the following.

UPDATE mytable 
SET myfield = 'value' 
WHERE other_field = 'other_value';

But what about if you want to update multiple rows but you need the field set to a different value for each row? For example, maybe you have a table of categories with a field to store their display order. How would you update the display order field for each category when the order changes? Most often you will see people just run multiple queries. For example, if you are using PHP you might think to do something like this:

foreach ($display_order as $id => $ordinal) {     
    $sql = "UPDATE categories 
            SET display_order = $ordinal 
            WHERE id = $id";     

Of course this will work fine, but queries in loops make me start to sweat nervously. It’s all to easy to start hammering the database with a crazy number of queries. Fortunately there is a better way! The SQL is a little more complex, but it is possible to achieve the same result using only one query. The syntax is as follows.

UPDATE mytable     
SET myfield = CASE other_field 
              WHEN 1 THEN 'value'         
              WHEN 2 THEN 'value'         
              WHEN 3 THEN 'value'     
WHERE id IN (1,2,3)

Mapping this to the categories example, we get the following query.

UPDATE categories     
SET display_order = CASE id 
                    WHEN 1 THEN 3         
                    WHEN 2 THEN 4         
                    WHEN 3 THEN 5     
WHERE id IN (1,2,3)

This is fairly simple to understand. Rather than setting a field to a particular value, a CASE operator is used to determine which of a set of values is used based on a given condition, in this case if the value of the id field matches the specified id.

The WHERE clause is not needed as such in that it doesn’t affect the logic of the query, but it does improve performance by ensuring that the logic is only applied to the smallest number of rows possible. In this example only 3 rows are being updated, and the WHERE clause ensures that only those 3 rows are tested. Without it the every row in the table would be tested (unnecessarily, since they will never match).

What about when you need to update multiple fields? This is easily done just by adding another CASE block.

UPDATE categories     
SET display_order = CASE id         
                    WHEN 1 THEN 3         
                    WHEN 2 THEN 4         
                    WHEN 3 THEN 5     
    title         = CASE id         
                    WHEN 1 THEN 'New Title 1'         
                    WHEN 2 THEN 'New Title 2'         
                    WHEN 3 THEN 'New Title 3'     
WHERE id IN (1,2,3)

Now this is all well and good, but the real beauty of this comes when the technique is combined with a scripting language such as PHP in order to build these queries dynamically. Let’s examine one technique for doing this, using the category ordering example.

// An array containing the category ids as keys and the new positions as values 
$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_order as $id => $ordinal) {     
    $sql .= sprintf("WHEN %d THEN %d ", $id, $ordinal); 
$sql .= "END WHERE id IN ($ids)"; 
echo $sql;

Now in this particular example, only 8 rows are being updated, but 7 queries have been trimed, which is not insignificant. Apply this technique to a situation where hundreds or thousands of rows have to be updated and you can imagine the benefits it will have.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s