Update multiple rows (with different ids and different values) using one query I want to update multiple rows in a custom table using just one query.

Each row has its own id (obviously) and different fields/values to update.

Given this question and this post, i think i can achieve it with a single db_query, similar to this one:

UPDATE {table} SET

`field1` = CASE `id`
    WHEN 1 THEN 'field1 value 1'
    WHEN 2 THEN 'field1 value 2'
    END

`field2` = CASE `id`
    WHEN 1 THEN 'field2 value 1'
    WHEN 2 THEN 'field2 value 2'
    END

WHERE `id` IN(1, 2);


Or:

INSERT INTO {table} (`id`, `field1`, `field2`)
VALUES
    (1, 'field1 value 1', 'field2 value 1'),
    (2, 'field1 value 2', 'field2 value 2'),
ON DUPLICATE KEY UPDATE
    `id` = VALUES(`id`),
    `field1` = VALUES(`field1`),
    `field2` = VALUES(`field2`);


But i'ld feel more confortable using db_update instead of db_query.

Any suggestions? Thanks
You can use below example to update:

    $items = array(1,2);
    db_update('your_table')-&gt;fields(array(
      'update_field_one' =&gt; $to_update_one,
      'update_field_two' =&gt; $to_update_two,
      'updated' =&gt; REQUEST_TIME,
    ))-&gt;condition('id', $items, 'IN')-&gt;execute();