On the sorting problem of a field in a database, how to implement the sort without repetition

Source: Internet
Author: User
Tags repetition
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.

sqlUPDATE 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:

sqlORDER BY sort DESC, modify_time DESC
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.