编辑:人们很难理解我想要什么。所以这里有漂亮的图片来解释它的细节。
首先将Transactions加入Strange:
到目前为止的结果
Customer Invoice TransactionID Mass Length LeptonNumber
======== ======= ============= ==== ==================== ============
Ian One 1 Ian Judgement Spaulders 50
Ian One 1 Ian Glorious Breastplate 50
Chris Two 2 Chris Barenavel 2
现在尝试使用Down连接剩余的行:
到目前为止的结果
Customer Invoice TransactionID Mass Length LeptonNumber
======== ======= ============= ==== ==================== ============
Ian One 1 Ian Judgement Spaulders 50
Ian One 1 Ian Glorious Breastplate 50
Chris Two 2 Chris Barenavel 2
Jamie Krol Blade 3 Jay Krol Blade 90
Jay Arcanite Reaper 4 Ian Arcanite Reaper 90
最后,将剩余的行加入Charmed:
到目前为止的结果
Customer Invoice TransactionID Mass Length LeptonNumber
======== ======= ============= ==== ==================== ============
Ian One 1 Ian Judgement Spaulders 50
Ian One 1 Ian Glorious Breastplate 50
Chris Two 2 Chris Barenavel 2
Jamie Krol Blade 3 Jay Krol Blade 90
Jay Arcanite Reaper 4 Ian Arcanite Reaper 90
Potatoe Dan Quayle 5 Potatoe Dan Quayle 90
以及如何查看我们剩下的行:
给我我想要的结果集
Customer Invoice TransactionID Mass Length LeptonNumber
======== ======= ============= ==== ==================== ============
Ian One 1 Ian Judgement Spaulders 50
Ian One 1 Ian Glorious Breastplate 50
Chris Two 2 Chris Barenavel 2
Jamie Krol Blade 3 Jay Krol Blade 90
Jay Arcanite Reaper 4 Ian Arcanite Reaper 90
Potatoe Dan Quayle 5 Potatoe Dan Quayle 90
Stapler Alexstraza 6 NULL NULL NULL
我有一个主表:
Transactions
+----------+
| |
| |
| |
| |
| |
| |
| |
+----------+
我希望此表中的每一行仅加入一个可能的匹配表:
Tranasctions Strange
+----------+ +----------+
| row 1 ===|=====>| row 1 | Down
| row 2 ===|=====>| row 2 | +---------+
| row 3 ===|======+----------+======>| row 1 | Charmed
| row 4 ===|========================>| row 2 | +---------+
| row 5 ===|=========================+---------+======>| row 1 |
| row 6 ===|==========================================>| row 2 |
+----------+ +---------+
通常我会作为Transactions
以下集合的连接执行Strange || Down || Charmed
:
SELECT
Transactions.*,
Quarks.Mass,
Quarks.Length,
Quarks.LeptonNumber
FROM Transactions
INNER JOIN NationalSecurityLetters
ON Transactions.TransactionID = NationalSecurityLetters.ReferenceNumber
LEFT JOIN (
SELECT 'Strange' AS Type, * FROM Strange
UNION ALL
SELECT 'Down' AS Type, * FROM Down
UNION ALL
SELECT 'Charmed' AS Type, * FROM Charmed
) Quarks
ON (
(Quarks.Type = 'Strange' AND Transactions.Customer = Quarks.Mass)
OR
(Quarks.Type = 'Down' AND Transactions.Invoice = Quarks.Length)
OR
(Quarks.Type = 'Charmed' AND Transactions.Customer = Quarks.Length)
)
问题是我希望加入以首选顺序发生:
Strange
Down
Charmed
单个事务完全有可能在多个表中具有匹配的条目。但是对于其他表的每个可能的JOIN,Transactions
我希望 SQL Server更喜欢该Strange
表。如果没有匹配,然后去Down
表。如果没有匹配Charmed
上桌。
If you find a match in Prefer the matching row from
========================== ============================
Strange Strange
Strange and Down Strange
Strange, Down, and Charmed Strange
Down Down
Down and Charmed Down
Charmed Charmed
(no match?) (then there's no match)
我考虑过使用一个OPTION(FORCE ORDER)
子句:
SELECT *
FROM Transactions
INNER JOIN NationalSecurityLetters
ON Transactions.TransactionID = NationalSecurityLetters.ReferenceNumber
LEFT JOIN (
SELECT 'Strange' AS Type, * FROM Strange
UNION ALL
SELECT 'Down' AS Type, * FROM Strange
UNION ALL
SELECT 'Charmed' AS Type, * FROM Strange
) Quarks
ON (
(Quarks.Type = 'Strange' AND Transactions.Customer = Quarks.Mass)
OR
(Quarks.Type = 'Down' AND Transactions.Invoice = Quarks.Length)
OR
(Quarks.Type = 'Charmed' AND Transactions.Customer = Quarks.Length)
)
OPTION (FORCE ORDER)
但我不想强制 SQL Server 加入
Transactions
==>NationalSecurityLetters
,当加入可能更有利时NationalSecurityLetters
==>Transactions