Created
April 12, 2013 14:15
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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