Skip to content

Instantly share code, notes, and snippets.

@andrewwheal
Created April 12, 2013 14:15
Show Gist options
  • Select an option

  • Save andrewwheal/5372319 to your computer and use it in GitHub Desktop.

Select an option

Save andrewwheal/5372319 to your computer and use it in GitHub Desktop.
Update a sequential int order field based on moving something with an ID to a specific position.
SET @id = 3;
SET @to = 9;
SET @lower = IF(@from < @to, @from, @to);
SET @upper = IF(@from < @to, @to, @from);
UPDATE `ordering`
SET `order` =
IF(`id` = @id,
@to,
IF(`order` <= @upper AND `order` >= @lower,
IF(@from < @to,
`order` - 1,
`order` + 1
),
`order`
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment