On Wed, 29 Jan 2003, Paul Makepeace wrote: > So there are a bunch of things "order by rank". I'd like to implement a > move up/down in SQL. So say the target was id=20 moving up, I'd like its > rank to become 1, and id=10's rank to become 2.
$query = "UPDATE fw SET rank=rank+(-2*(rank-($position+0.5))) WHERE "; $query .= "(rank=$position OR rank=$position+1)" So says some of my long-running production code. Actually, my interface talks about moving a rule up or down, rather than swapping a pair of rules. $position is the rule you want to move down (ie swap with the one below it) A quick... if ($direction eq "up") { $position--; } moves the rule the other direction. It does require that ranks are contiguous, and that the interface doesn't let them move the bottom element down, or the top element up. the hatter