'This guide shows how to update a column in MySQL sequentially based on a specific order. It is commonly used to reset ordering values in tables.', 'howto' => [ 'Initialize a counter variable.', 'Run UPDATE query with ORDER BY.', 'Verify that values are assigned sequentially.' ], 'why' => 'This method helps maintain ordered data such as image lists, menu positions, or custom sorting fields.', 'faq' => [ [ 'q' => 'What is @cnt in MySQL?', 'a' => 'It is a user-defined variable used to generate sequential numbers.' ], [ 'q' => 'Does ORDER BY work in UPDATE?', 'a' => 'Yes, MySQL supports ORDER BY in UPDATE statements.' ], [ 'q' => 'Can this break existing order?', 'a' => 'Yes, it overwrites values, so use carefully.' ], [ 'q' => 'Is this safe for production?', 'a' => 'Test first before running on production data.' ] ] ]; include_once $_SERVER['DOCUMENT_ROOT'] . '/inc/guides/related-guides-helper.php'; include_once $_SERVER['DOCUMENT_ROOT'] . '/inc/guides/related-tools-helper.php'; $relatedGuides = getAutoRelatedGuides($guideSlug, $guideCategory, 5); $relatedTools = getRelatedToolsForGuide($guideSlug, 5); include_once $_SERVER['DOCUMENT_ROOT'] . "/inc/header.html"; ?>

Guides

Step-by-step tutorials and practical fixes for servers, systems, databases, and daily technical work.
Database Guides

Update MySQL Order Column Sequentially

Assign ordered values using a single SQL query.

Before you start

Backup your data before running UPDATE queries.
Make sure ORDER BY condition is correct.
Test on a small dataset first.

Step-by-step

SQL
SET @cnt = 0;

UPDATE wp_bwg_image
SET `order` = @cnt := @cnt + 1
WHERE gallery_id = '12'
ORDER BY `id` DESC;
What it does

Assigns sequential numbers based on id descending order.

Common issues

Wrong order

Check ORDER BY condition carefully.

Unexpected values

Make sure @cnt is initialized.

Data overwritten

This query updates all matching rows.