我需要从一个表(这是一个CRM库存)到另外四个表(它们是webserver库存)进行比较。目前我正在使用以下查询,但我得到重复和误报,因为它是重新检查第一个表对着彼此的表,只是每次输出结果然后一起显示它们:
SELECT sc.Client,
ws.[Host Header],
ws.[IP Address],
ws.[Status]
FROM sc
FULL JOIN ws
ON sc.Urls = ws.[Host Header]
WHERE sc.Client IS NULL
OR ws.[Host Header] IS NULL
OR sc.Urls <> ws.[Host Header]
UNION
SELECT sc.Client,
ws2.[Host Header],
ws2.[IP Address],
ws2.[Status]
FROM sc
FULL JOIN ws2
ON sc.Urls = ws2.[Host Header]
WHERE sc.Client IS NULL
OR ws2.[Host Header] IS NULL
OR sc.Urls <> ws2.[Host Header]
UNION
SELECT sc.Client,
ws3.[Host Header],
ws3.[IP Address],
ws3.[Status]
FROM sc
FULL JOIN ws3
ON sc.Urls = ws3.[Host Header]
WHERE sc.Client IS NULL
OR ws3.[Host Header] IS NULL
OR sc.Urls <> ws3.[Host Header]
UNION
SELECT sc.Client,
ws4.[Host Header],
ws4.[IP Address],
ws4.[Status]
FROM sc
FULL JOIN ws4
ON sc.Urls = ws4.[Host Header]
WHERE sc.Client IS NULL
OR ws4.[Host Header] IS NULL
OR sc.Urls <> ws4.[Host Header]
有人能指出我正确的方向吗?我想只返回sc.Client和ws。[Host Header]字段中每个表为null或不匹配的表的不同结果,并且4个webserver表之间没有重复。
谢谢!
答案 0 :(得分:0)
为什么不首先尝试在所有ws表之间执行UNION,然后针对UNION操作产生的表表达式执行sc表的FULL JOIN? - Giorgos Betsos
这已经死了!我将查询调整为以下内容,一切都很顺利!
SELECT sc.Client, ws.[Host Header], ws.[IP Address], ws.[Status]
FROM sc full outer join (select * from ws1 union all select * from ws2 union all select * from ws3 union all select * from ws4) as ws ON sc.Urls = ws.[Host Header]
谢谢!