I have been doing some searching but have not been able to find an answer for this so thought I would ask here as the people here know everything 🙂
I am trying to combine these 2 update queries into one query.
UPDATE addresses SET is_default='0' WHERE id!='1'
UPDATE addresses SET is_default='1' WHERE id='1'
I assume this should be too hard to accomplish but i cant seem to work it out 🙁
Thanks
Paul
You can use
CASEto do this:In your example, you’re updating the entire table, so you wouldn’t benefit from indexes no matter what, but if you were only doing a subset of values, you’d still want to leave the where in (because its very doubtful the optimizer could figure out how to use the index). For example:
(Note the two different CASE syntaxes).