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

如果 join 返回 null,則在默認行上加入

If join returns null then join on default row(如果 join 返回 null,則在默認行上加入)
本文介紹了如果 join 返回 null,則在默認行上加入的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試將 2 個表連接到一行,如果該行為空,則將該行連接到默認行.

I am trying to join 2 tables on a row, and if that row is null, then join that row to the default row.

表 1:事件

EventID     EventName
----------- -------------
1           January
2           February
3           March
4           April

表 2:菜單

MenuID     EventID     MenuVersion     
---------- ----------- --------------- 
1                      1
2          3           2
3          4           4
4                      4

我的嘗試

SELECT * FROM Events
LEFT JOIN Menus
ON Events.EventID = Menus.EventID

我得到的輸出

EventID     EventName     MenuID    EventID    MenuVersion
----------- ------------- --------- ---------- ---------------
1           January                           
2           February                          
3           March         2         3          2
4           April         3         4          4

在這種情況下,Menus 表的默認行是具有最高 MenuID 和空 EventID 的行.

The default row of the Menus table in this case is the row with the highest MenuID and a null EventID.

我想要的輸出

EventID     EventName     MenuID    EventID    MenuVersion
----------- ------------- --------- ---------- ---------------
1           January       4                    4
2           February      4                    4
3           March         2         3          2
4           April         3         4          4

推薦答案

交叉應用默認行,并在沒有行被left join打開時使用其值.

Cross apply the default row and use its values when no row is left joined on.

DECLARE @Events TABLE (EventId INT, EventName VARCHAR(12));
DECLARE @Menus TABLE (MenuId INT, EventId INT, MenuVersion INT);

INSERT INTO @Events (EventId, EventName)
VALUES
(1, 'January'),
(2, 'February'),
(3, 'March'),
(4, 'April');

INSERT INTO @Menus (MenuId, EventId, MenuVersion)
VALUES
(1, null, 1),
(2, 3, 2),
(3, 4, 4),
(4, null, 4);

SELECT E.EventId, E.EventName, COALESCE(M.MenuId, D.MenuId) MenuId, M.EventId, COALESCE(M.MenuVersion, D.MenuVersion) MenuVersion
FROM @Events E
LEFT JOIN @Menus M ON M.EventID = E.EventID
CROSS APPLY (SELECT TOP 1 * FROM @Menus WHERE EventId IS NULL ORDER BY MenuId DESC) D;

按要求返回:

EventId EventName   MenuId  EventId MenuVersion
1       January     4       NULL    4
2       February    4       NULL    4
3       March       2       3       2
4       April       3       4       4

注意:如果您將來使用 DDL/DML 語句提出這樣的問題,您會得到更快的答復,因為它使人們不必全部輸入.

Note: If you set out your questions like this in future with the DDL/DML statements you'll get a much faster response because it saves people from having to type it all in.

這篇關于如果 join 返回 null,則在默認行上加入的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 国产乱码精品1区2区3区 | 久久综合一区二区三区 | 中文字幕在线中文 | 亚洲精品www | 天堂中文在线播放 | 久久久区 | 日韩中文一区二区 | 黄色大片视频 | 在线免费小视频 | 亚洲一区在线播放 | 中文字幕国产日韩 | 最新中文字幕在线播放 | 午夜视频网站 | 免费不卡视频 | 高清国产一区二区 | 中文字幕av一区二区三区 | 免费黄色a级毛片 | 激情综合五月 | 精品91视频 | 久久久久午夜 | 视频三区| 免费在线观看av | 国产一区二区黑人欧美xxxx | 亚州精品天堂中文字幕 | 成人欧美一区二区三区在线观看 | 亚洲成人一区二区 | 日韩在线不卡 | 亚洲天堂久久新 | 一区二区三区四区在线视频 | 午夜精品一区二区三区在线视频 | 久久精品欧美一区二区三区不卡 | 视频在线亚洲 | 成人免费影院 | 午夜精品一区二区三区免费视频 | 美女一级毛片 | 精品久久久网站 | 韩日视频在线观看 | 国内精品一区二区三区 | 色视频成人在线观看免 | 日韩免费视频一区二区 | 性一交一乱一透一a级 |