Update MySQL Order Column Sequentially
Assign ordered values using a single SQL query.
Before you start
Step-by-step
SET @cnt = 0;
UPDATE wp_bwg_image
SET `order` = @cnt := @cnt + 1
WHERE gallery_id = '12'
ORDER BY `id` DESC;
Assigns sequential numbers based on id descending order.
Common issues
Check ORDER BY condition carefully.
Make sure @cnt is initialized.
This query updates all matching rows.