I need to update the display_order column with sequence for numbers for array of ids Consider the following array of Ids (13, 6, 5, 19, 1, 3, 2), for this ids i need to update the order column
DB
+----+-------+---------------+
| id | name | display_order |
+----+-------+---------------+
| 1 | cat1 | 3 |
+----+-------+---------------+
| 2 | cat2 | 4 |
+----+-------+---------------+
| 5 | cat5 | 6 |
+----+-------+---------------+
| 6 | cat6 | 1 |
+----+-------+---------------+
| 13 | cat13 | 2 |
+----+-------+---------------+
Currently im using the following query
UPDATE categories
SET display_order = CASE id
WHEN 13 THEN 1
WHEN 6 THEN 2
WHEN 5 THEN 3
WHEN 19 THEN 4
WHEN 1 THEN 5
WHEN 3 THEN 6
WHEN 2 THEN 7
END
WHERE id IN (13, 6, 5, 19, 1, 3, 2);
Currently im looping the ids in PHP to generate the "case when" statement, the array may come with more Ids. Is there any alternative way to do it in Mysql so i can avoid looping.