我正在尝试将表“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”
答案 0 :(得分:0)
不需要UNION
或WHERE
。如果您知道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);