久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

基于位置重新排序行 SQL Server

Re-ordering rows based on position SQL Server(基于位置重新排序行 SQL Server)
本文介紹了基于位置重新排序行 SQL Server的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我在 SO 上看到了很多關于這個問題的問題,但沒有一個與我的場景相關.除了做基本的 CRUD 操作之外,我與 SQL 人員相去甚遠.因此我對此非常堅持.

I have seen many questions on SO regarding this question but none is relevant to my scenario. I am nowhere near an SQL guy apart from doing basic CRUD operations. Hence I am quite stuck with this.

我有一張桌子.

myTable [rID, newsID, OrderPosition] where;

rID is primaryKey int column, 
newsID int is the ID of an item from another table and,
OrderPosition int to hold the position of the rows. 

myTable 總是總共有 10 行.

myTable will always have a total of 10 rows.

所以,最初,讓我們假設 myTable 有以下數據:

So, initially, lets assume myTable has the following data:

rID    newsID    OrderPosition
100    4000      1
101    4100      2
102    4200      3
103    4300      4
104    4400      5
105    4500      6
106    4600      7
107    4700      8
108    4800      9
109    4900      10

預期的功能應該如下;

插入新內容

插入新項目時,用戶應該能夠將其插入到他/她想要的任何位置.現在,我只能通過刪除 OrderPosition = 10 行、將新記錄 OrderPosition 分配為 0 并重新排序表來設法將新記錄插入到第一個位置.但是客戶想要選擇項目應該放在哪個位置.在這種情況下,我假設 OrderPosition = 10 將再次被刪除?

when inserting a new item, user should be able to insert it into any position he/she desires. Right now, I only managed to insert the new record to the first position by deleting the OrderPosition = 10 row, assigning the new record OrderPosition of 0, and reorder the table. But client wants to select which position the item should go. In which case I assume, the OrderPosition = 10 will be deleted again?

刪除

當從這張表中刪除一條記錄時,由于總有10條記錄,所以我需要從另一個表[tblNews]中獲取最新輸入的記錄并將其插入到第10個位置(我可以得到最后一個tblNews 中的記錄按輸入日期降序排列.)由于我不知道他們將刪除哪條記錄,因此我不知道如何在刪除記錄后對表重新排序.

When a record from this table is deleted, since there will always be a total of 10 records, I need to get the latest entered record from another table [tblNews] and insert it to the 10th position (I can get the last record from tblNews by ordering descending by the date it was entered.) Since I don't know which record they will delete, I don't know how to re-order the table after a record has been deleted.

非常感謝任何幫助、代碼和文章指導.

Any help, code, direction to an article would be very much appreciated.

============編輯====================

=========== EDIT ====================

答案中提到的 UPDATE 方法對我不起作用,因為;例如用戶想要在第 5 個訂單位置插入一條新記錄.這意味著,訂單位置 10 將被刪除,訂單位置 5、6、7、8 和 9 的當前記錄將增加 1

The UPDATE method mentioned in the answers will not work for me since; e.g. user wants to insert a new record into the 5th order position. This would mean, the order position 10 would be deleted and the current records with order postions 5,6,7,8 and 9 is to be incremented by one

推薦答案

插入行時,需要移動"下面的行以騰出空間.例如,可以像這樣在位置 4 上插入:

When inserting the row, you need to "move" the rows below to make the room for it. For example, inserting on a position 4 could be done like this:

DELETE FROM myTable WHERE OrderPosition = 10; -- If you want the keep the table from growing beyond 10 rows.
UPDATE myTable SET OrderPosition = OrderPosition + 1 WHERE OrderPosition >= 4;
INSERT INTO myTable VALUES (..., 4);

相反,在刪除該行后,您可以將其下方"的行移回:

Conversely, after you delete the row, you can move the rows "below" it back up:

DELETE FROM myTable WHERE OrderPosition = 4;
UPDATE myTable SET OrderPosition = OrderPosition - 1 WHERE OrderPosition > 4;
-- Now you can insert the row on position 10 if desired.

注意,即使 OrderPosition 有一個 UNIQUE 約束,UPDATE 也不會違反它.

Note that even if OrderPosition has a UNIQUE constraint on it, UPDATE won't violate it.

[SQL 小提琴]

這篇關于基于位置重新排序行 SQL Server的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創建視圖?)
Create calculated value based on calculated value inside previous row(根據前一行內的計算值創建計算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 蜜桃精品在线 | 成人黄色电影在线播放 | 日韩在线不卡 | 成人在线观看免费 | 国产精品久久精品 | 美日韩视频 | 亚洲第一中文字幕 | 日韩欧美国产精品 | 国产激情综合五月久久 | 日本久久精品视频 | www久久久| 国产精品18hdxxxⅹ在线 | 亚洲成人一区二区 | 国产精品日韩欧美一区二区三区 | 久久久久网站 | 亚洲日韩中文字幕一区 | 成人欧美一区二区三区色青冈 | 美女在线观看国产 | 91小视频在线 | 91久久国产| 国产区久久 | 欧美成人激情 | 精品日韩| 久操伊人| 精品一区二区免费视频 | 国产精品美女 | 色婷婷av99xx | 水蜜桃亚洲一二三四在线 | 国产免费一区二区三区免费视频 | 自拍偷拍第一页 | 成人av在线播放 | 精精精精xxxx免费视频 | 国产福利网站 | 综合国产第二页 | 欧美国产日韩一区二区三区 | 91免费观看视频 | 日韩欧美一级 | 日韩一区二区在线视频 | 最新中文在线视频 | 欧美精品欧美精品系列 | 午夜色婷婷 |