这是我的情况。
PayTable
+-------+------+--------------+
| Craft | Job | sequence |
+-------+------+--------------+
| 400 | 1 | 1 |
+-------+------+--------------+
| 401 | 2 | 2 |
+-------+------+--------------+
| 5501 | 2 | 3 |
+-------+------+--------------+
Jobs
+-------+------+------+
| Job | CraftTemplate |
+-------+------+------+
| 1 | 1 |
+-------+------+------+
| 2 | 1 |
+-------+------+------+
Pay Template
+-------+--+
| Template |
+-------+--+
| 1 |
+-------+--+
PayCraftTemplate
+-------+------+---------+
| PayTemplate | Craft |
+-------+------+---------+
| 1 | 400 |
+-------+------+---------+
| 1 | 401 |
+-------+------+---------+
我需要做的是从 PayTable 中找到 PayCraftTemplate 中不存在该工艺的所有工艺。作为一种反连接模式,这似乎非常简单,但我似乎无法让数据正确返回。
加入链接如下:
PayTable INNER JOIN Jobs by Job -> Job
Jobs LEFT OUTER JOIN Pay Template by CraftTemplate -> Template
Pay Template LEFT OUTER JOIN by Template -> PayTemplate
这是我目前的尝试:
select
*
FROM
PayTable
WHERE NOT EXISTS (
SELECT 1
FROM
Jobs
LEFT OUTER JOIN PayTemplate
ON PayTemplate.Template = Jobs.CraftTemplate
LEFT OUTER JOIN PayCraftTemplate
ON PayCraftTemplate.Template = PayTemplate.Template
WHERE
PayTable.Craft = PayCraftTemplate.Craft AND PayTable.Job = Jobs.Job
) AND PayTable.Job IS NOT NULL AND PayTable.Craft IS NOT NULL
这没有返回我期望的数据,我希望 PayTable 的第 3 行只返回而不是我得到第 1,2 行