我想将Informants
表加入到他们对应的Handlers
:
Informants Handlers
+------------------------+ +----------------------------------------+
|Name Type | | HandlerID Name HandlerType |
|------------ ---------- | |---------- ----------------------------|
|Ronald Regan Politician | | 1 J. Edgar FBI |
|Sal Vitale MOB | | 2 Charles Cabell CIA |
|Elia Kazan Famous | | 3 Allen Dulles CIA |
|Mrs. Kravitz Citizen | | 7 Joe McCarthy Congressman |
|White Pawn Foreign | +----------------------------------------+
+------------------------+
我遵循的规则取决于谁来确定您可以通知的人群:
- 政治家 --> 中央情报局
- 黑手党和著名的名人 --> 联邦调查局
- 普通公民 --> 当地警察局
- 外国特工 --> 可以求助于 NSA
因此,我将告密者与他们可以通知的对象进行匹配:
SELECT
Informants.Name AS RatName,
Informants.Type AS RatType,
Handlers.Name AS GmanName,
Handlers.HandlerID
FROM Informants
LEFT JOIN Handlers
ON (
(Informants.Type IN ('Politician') AND Handlers.HandlerType = 'CIA')
OR
(Informants.Type IN ('MOB', 'Famous') AND Handlers.HandlerType = 'FBI')
OR
(Informants.Type IN ('Citizen') AND Handlers.HandlerType = 'Police')
OR
(Informants.Type IN ('Foreign') AND Handlers.HandlerType = 'NSA')
)
我得到了结果:
RatName RatType GmanName HandlerID
============= ============== ============== =========
Ronald Regan Politician Charles Cabell 2
Ronald Regan Politician Allen Dulles 3
Sal Vitale MOB J. Edgar 1
Elia Kazan Famous J. Edgar 1
Mrs. Kravitz Citizen NULL NULL
White Pawn Foreign NULL NULL
您可以在此处看到Ronald Regan有两个可以求助的已配置处理程序。尽管:
- 克拉维茨夫人,和
- 白典当
没有他们可以通知的处理程序。
所以我现在想要的是处理程序的后备列表。如果您是代理Politician
或Foreign
代理,我们希望您回退FBI
处理程序。这意味着理想情况下我的结果集将是:
RatName RatType GmanName HandlerID
============= ============== ============== =========
Ronald Regan Politician Charles Cabell 2
Ronald Regan Politician Allen Dulles 3
Sal Vitale MOB J. Edgar 1
Elia Kazan Famous J. Edgar 1
Mrs. Kravitz Citizen NULL NULL
White Pawn Foreign J. Edgar 1
那是我的问题,我需要加入早期加入不匹配任何内容的标准。
问题
我试图避免的问题是:
- 主要案例连接
Politicians
到FBI
- 备份案例加入
Politicians
到CIA
我最终是这样的:
RatName RatType GmanName HandlerID
============= ============== ============== =========
Ronald Regan Politician Charles Cabell 2
Ronald Regan Politician Allen Dulles 3
Ronald Regan Politician J. Edgar 1
Sal Vitale MOB J. Edgar 1
Elia Kazan Famous J. Edgar 1
Mrs. Kravitz Citizen NULL NULL
White Pawn Foreign J. Edgar 1
罗纳德·里根(Ronald Regan)获得了一个新的“后备”条目,当时他已经有人要向他报告。