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

SQL 在兩表排列中向上或向下移動行

SQL to move rows up or down in two-table arrangement(SQL 在兩表排列中向上或向下移動行)
本文介紹了SQL 在兩表排列中向上或向下移動行的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有兩個以這種方式設計的表格,考慮到可能會重新排列元素:

I have two tables that I designed this way with a possible reshuffling of elements in mind:

1. [dbo.test_db_002] with columns:
[id] = INT NOT NULL IDENTITY(1,1) PRIMARY KEY
[name] = NVARCHAR(255)

2. [dbo.test_db_003] with columns:
[ord] = INT
[itmid] = INT NOT NULL PRIMARY KEY

[itmid] 列有一個約束,將其鏈接到 [dbo.test_db_002].[id],如下所示:

[itmid] column has a constraint linking it to [dbo.test_db_002].[id] like so:

ALTER TABLE [dbo.test_db_003] 
ADD CONSTRAINT fk1 FOREIGN KEY ([itmid]) 
REFERENCES [dbo.test_db_002]([id]) 
ON DELETE CASCADE ON UPDATE CASCADE;

比如說,[dbo.test_db_002] 表有以下數據:

Say, [dbo.test_db_002] table has the following data:

[id] [name] 
3    John
5    Mary
8    Michael
10   Steve
13   Jack
20   Pete

和 [dbo.test_db_003] 具有以下排序數據:

and [dbo.test_db_003] has the following ordering data:

[ord] [itmid]
1      5
4      8
5      13
8      3
10     10
13     20

因此,當我從數據庫中檢索名稱時,我使用以下 SQL:

So when I retrieve names from the database I use the following SQL:

SELECT [name]
FROM   [dbo.test_db_002] t1
LEFT JOIN [dbo.test_db_003] t2 ON t1.[id]=t2.[itmid]
ORDER BY t2.[ord] ASC

它生成名稱列表(按 [dbo.test_db_003].[ord] 列排序):

It produces the list of names (ordered by the [dbo.test_db_003].[ord] column):

Mary
Michael
Jack
John
Steve
Pete

我正在尋找一個選項,可以在列表中上下移動每個名稱.例如,如果我想將John"上移一位,我該怎么做?

What I am looking for is an option to move each of the names up and down the list. For instance, if I want to move "John" one position up, what do I do?

到目前為止,我想出了這個部分 SQL:

So far I came up with this partial SQL:

WITH cte AS
(
    SELECT [id], [ord], ROW_NUMBER() OVER (ORDER BY t2.[ord] ASC) AS rowNum
    FROM [dbo.test_db_002] t1
    LEFT JOIN [dbo.test_db_003] t2 ON t1.[id] = t2.[itmid]
)

這將選擇以下內容:

rowNum  [id]  [ord]
1        1     5
2        4     8
3        5     13
4        8     3
5        10    10
6        13    20

所以我知道我需要將 [ord] 列中的值從索引 3 開始向上移動一個(因為John"索引是 4),然后以某種方式將John"的 [ord] 設置為5,但是你是怎么做到的?

So I understand that I need to shift values in [ord] column up by one starting from the index 3 (since "John" index is 4) and then somehow make "John"'s [ord] to be set to 5, but how do you do that?

推薦答案

我準備了一個 完整演示 為您介紹這如何在 data.stackexchange.com 上工作.
該解決方案是根據您的評論量身定制的:

I prepared a complete demo for you how this can work on data.stackexchange.com.
The solution is tailored to your comment:

向上或向下移動只能是一步 - 換句話說,一個不能移動 2 個或更多位置

the move up or down can be only a single step - in other words, one cannot move 2 or more positions

在這個例子中,我讓約翰與他上方的杰克交易順序頭寸:

In the example I make John trade ordinal positions with Jack above him:

WITH x AS (
  SELECT t2.itmid, t2.ord
  FROM   dbo.test_db_002 t1
  LEFT   JOIN dbo.test_db_003 t2 ON (t1.id = t2.itmid)
  WHERE  t1.name = 'John'  -- must be unique, or query by id ...
  )
  , y AS (
  SELECT TOP 1
         t.itmid, t.ord
  FROM   dbo.test_db_003 t, x
  WHERE  t.ord < x.ord     -- smaller ord = "above"
  ORDER  BY t.ord DESC
  )
UPDATE dbo.test_db_003 SET ord = z.ord
FROM (
   SELECT x.itmid, y.ord FROM x,y
   UNION ALL
   SELECT y.itmid, x.ord FROM x,y
   ) z
WHERE  dbo.test_db_003.itmid = z.itmid   

###主要觀點:

  1. 使用兩個 CTE 來構建查詢:
  2. 獲取 John 的 id &順序位置
  3. 對他上面的人也一樣
  4. UNION ALL
  5. 的幫助下準備兩行,其中這兩個交換序數
  6. 在現在簡單的UPDATE
  7. 中使用這兩行
  1. Use two CTE to structure the query:
  2. Get John's id & ordinal position
  3. Get the same for the person above him
  4. Prepare two rows where these two switch ordinal numbers with the help of UNION ALL
  5. Use these two rows in a now simple UPDATE

  • 序號位置 ord 必須允許傳遞重復項才能使其工作.
  • 如果沒有人在 'above',則查詢將無聲無息地執行任何操作.
    • The ordinal position ord must allow passing duplicates for this to work.
    • If there is nobody 'above', the query will silently do nothing.
    • 這篇關于SQL 在兩表排列中向上或向下移動行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 亚洲日本国产 | 欧美性色网 | 天天插天天插 | 香蕉视频在线看 | 亚洲欧美视频一区 | 在线观看的av网站 | 美女黄色一级片 | 一区二区三区在线观看视频 | 日韩在线精品 | 青草网| 日本免费毛片 | 91久久久久久久久久 | 成人看片网站 | 成人午夜在线 | 国产成人精品一区二区 | av超碰在线 | 日韩成人在线观看视频 | 日本美女一级片 | 韩国免费理论片 | 色午夜| 亚洲欧美专区 | 一区二区三区网站 | 欧美性精品 | 天天操女人| a级片毛片| 91精品又粗又猛又爽 | 91亚洲精品乱码久久久久久蜜桃 | 久久精品在线视频 | 国产精品三级在线 | 在线播放一区 | 欧美精品一 | 97久久精品人人澡人人爽 | 91激情视频 | 国产午夜精品福利 | 色播亚洲 | 婷婷六月激情 | 欧美在线看片 | 亚洲毛片在线 | 成人毛片在线 | 在线视频亚洲 | 久久精品亚洲 |