简体   繁体   中英

Moving SQL record up or down Based on the number inserted or removed

I have a select list of records that have a number 1-10. The value for each <option> is the id from the record on the database and the text is another column value --- numToDo . (see below)

<select>
<option value="101">1</option>
<option value="102">2</option>
<option value="103">3</option>
<option value="104">4</option>
<option value="105">5</option>
...
</select>

I want to allow the user to add/remove and readjust items from the list, but I want the list to remain in order if 1-10. In other words if the person thinks record 105 should be completed before 102 I would like the current 2-(102) to be moved down and have the rest of those records 104,105 etc to change as well. in other words have it look like this:

<select>
<option value="101">1</option>
<option value="105">2</option>
<option value="102">3</option>
<option value="103">4</option>
<option value="104">5</option>
....

There are only ever 10 items or less listed at a time, and when they get completed the numToDo for the record becomes a 0 which gets moved out of the list and the numbers should be moved up as well, but if I can just get help with the first part I mentioned I can figure the rest out on my own.

I'm assuming this would be a query on the database end where it takes in the ID and the new number and updates the current row and then updates the rest based on the number affected? I don't really work on SQL so i'm not really sure if this can be done.

Use a record you can sort on, and when you move a record to a new spot, just ensure that the record sorting places it at the right spot. If you eg have this ordering

          Sorting 
Record#    order
  101        1
  102        2
  103        3
  104        4
  105        5

you can move record 105 to the second spot by only updating one sorting key like this.

          Sorting 
Record#    order
  101        1
  105        1.5   < average of record 101 and 102's Sorting key
  102        2
  103        3
  104        4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM