Updating many records at a time
| From: | Steve McGill | Date: | Tue, 01 Mar 2005 10:23:50 +0000 |
| Subject: | Updating many records at a time | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-38723@lists.php.net to get a copy of this message | ||
I have a situation where I am doing lots of complex sorting on records that
already exist in a MySQL table. The table has a 'sort_order' field which
means I can do a simple ORDER BY sort_order to keep it nice and quick on
SELECT, it's only UPDATE which is slow.
If I change the order that I want 10000 rows to be displayed, I am currently
making 10000 SQL queries such as:
UPDATE products SET sort_order=0 WHERE id='5';
UPDATE products SET sort_order=1 WHERE id='2';
UPDATE products SET sort_order=2 WHERE id='32';
and so on.
Obviously I'd love to be able to put all of this into one query.
Is there some way I can combine this with IN() ?
UPDATE products set sort_order=**** WHERE id IN('5','2','32');
Many thanks in advance for any help / advice.
Best wishes,
Steve