Answer the question
In order to leave comments, you need to log in
Why does the result of the selection change when putting down the MySQL index?
There is a table with columns id, sort and sort can go out of order. There is a slightly confusing query for moving a line to any position and shifting the sort values of the previous / next element:
SET @start=37;
UPDATE tablename m INNER JOIN (SELECT @start AS oldsort, (@start:=testsort.sort) AS newsort from tablename AS testsort WHERE testsort.sort BETWEEN @start AND 39 ORDER BY testsort.sort DESC ) p ON m.sort = p.oldsort SET m.sort = newsort WHERE sort<=39 AND sort>37;
UPDATE tablename SET sort=39 WHERE id={ид_перемещаемого элемента}
Answer the question
In order to leave comments, you need to log in
I don't know why it works without an index, but I know why it doesn't work for a field with an index
When comparing BETWEEN, the condition BETWEEN 37 AND 39 falls on the first row, and BETWEEN 39 AND 39 on the second. And, accordingly, nothing falls under this condition.
That is, for everything to work, you need BETWEEN 37 AND 39 or BETWEEN 37 AND @start
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question