Assuming that the database has 1-10 of the sort, now I want to insert a 1 sort, the data of this field is: 1,1,2,3,4,5,6,7,8,9,10, how to insert a duplicate value automatically +1, to ensure that the field data unique and orderly ordering, the final result is: 1, 2,3,4,5,6,7,8,9,10,11;
Add: The data may be like this: 1,2,2,3,4,5,5,5,6,6,8,9,9,99; when I insert a 1, or 2, the data is automatically ordered and guaranteed to be unique, assuming that 2 is inserted, and the result is: 1, 2,3,4,5,6,7,8,9,10,11,12,13,14,99
Reply content:
Assuming that the database has 1-10 of the sort, now I want to insert a 1 sort, the data of this field is: 1,1,2,3,4,5,6,7,8,9,10, how to insert a duplicate value automatically +1, to ensure that the field data unique and orderly ordering, the final result is: 1, 2,3,4,5,6,7,8,9,10,11;
Add: The data may be like this: 1,2,2,3,4,5,5,5,6,6,8,9,9,99; when I insert a 1, or 2, the data is automatically ordered and guaranteed to be unique, assuming that 2 is inserted, and the result is: 1, 2,3,4,5,6,7,8,9,10,11,12,13,14,99
Read the wrong question, you should judge whether to repeat the update it.
sql
UPDATE test SET sort=sort+1 WHERE sort>1;
In fact, you want to implement this sort function, not necessarily the sort field is unique.
You add one more field: Modify_time, recording the modification time.
Then your sort depends on sort and modify_time on OK:
sql
ORDER BY sort DESC, modify_time DESC