我在哪里弄乱了这个外部联盟?

时间:2014-04-25 16:18:33

标签: sql ms-access access-vba

我正在尝试将表“OP”中的1列添加到表“density”。

SELECT density.*, OP.[OP %]
FROM density 
LEFT JOIN OP ON (density.[Billet Number] = OP.[Billet Number] 
             AND density.Specimen = OP.Specimen)
UNION
SELECT density.*,OP.[OP %]
FROM density 
RIGHT JOIN OP ON (density.Specimen = OP.Specimen 
           AND density.[Billet Number] = OP.[Billet Number])
WHERE density.Specimen IS NULL 
      AND density.[Billet Number] IS NULL;
OP有12行,密度有~150。当我运行查询时,列“OP%”被添加到表中,但是12个值是新行,而不是“Specimen”和“Billet Number”

1 个答案:

答案 0 :(得分:0)

不需要UNIONWHERE。如果您知道OP中的行与Density中的行匹配,则可以执行INNER JOIN

SELECT density.*, OP.[OP %]
FROM density 
INNER JOIN OP ON (density.[Billet Number] = OP.[Billet Number] 
             AND density.Specimen = OP.Specimen);

如果可能存在不匹配但仍需要density中的所有行(如果没有匹配时[OP %]缺少值),则使用LEFT JOIN

SELECT density.*, OP.[OP %]
FROM density 
LEFT JOIN OP ON (density.[Billet Number] = OP.[Billet Number] 
             AND density.Specimen = OP.Specimen);