A simple question I’ve been asked:
Is it possible to merge two UPDATE queries, each on different WHERE conditions, into a single query?
For example, is it possible to merge the following two UPDATE statements into one?
mysql> UPDATE film SET rental_duration=rental_duration+1 WHERE rating = 'G'; Query OK, 178 rows affected (0.01 sec) mysql> UPDATE film SET rental_rate=rental_rate-0.5 WHERE length < 90; Query OK, 320 rows affected (0.01 sec)
To verify our tests, we take a checksum:
mysql> pager md5sum PAGER set to 'md5sum' mysql> SELECT film_id, title, rental_duration, rental_rate FROM film ORDER BY film_id; c2d253c3919efaa6d11487b1fd5061f3 -
Obviously, the following query is incorrect: Continue reading » “Multi condition UPDATE query”