Access 2013 - 一对多关系:在“一”侧显示所有值,仅在“多”侧选择值

时间:2015-04-02 15:46:10

标签: sql ms-access access-vba ms-access-2013

我有两个表格 - tblSalaryBandtblSalaryBandYeartblSalaryBand.ID = tblSalaryBandYear.SalaryBandID链接。在tblSalaryBandYear内,我有BandMax和BandMin的值,然后是ReviewYearID。因此,对于每个工资乐队,我每年都可以获得最高和最低工资。

我想要的是列出所有薪资范围以及所选年份的最高工资和最低工资。但是,tblSalaryBandYear中可能还没有该年薪资带的值。

我有一个连续的表单设置,即使他们使用左连接在tblSalaryBandYear中没有值,我也可以显示所有工资带:

SELECT tblSalaryBand.BandName, tblSalaryBandYear.BandMax, tblSalaryBandYear.BandMin, tblSalaryBand.ID AS SalaryBandID
FROM tblSalaryBand LEFT JOIN tblSalaryBandYear ON tblSalaryBand.ID = tblSalaryBandYear.SalaryBandID;

但是,当我尝试按照ReviewYearID过滤它们时,如果tblSalaryBandYear中不存在任何值,则它们根本不会显示。

SELECT tblSalaryBand.BandName, tblSalaryBandYear.BandMax, tblSalaryBandYear.BandMin, tblSalaryBand.ID AS SalaryBandID
FROM tblSalaryBand LEFT JOIN tblSalaryBandYear ON tblSalaryBand.ID = tblSalaryBandYear.SalaryBandID
WHERE (((tblSalaryBandYear.ReviewYearID)=[Forms]![frmHome]![SelectedYear]));

有没有办法让所有的Salary Bands显示出来,然后只显示选定的复习年份的BandMin和Max的值?

1 个答案:

答案 0 :(得分:0)

问题是where子句有效地消除了没有任何匹配的ReviewYearID的行(并使连接表现得像内连接而不是左外连接)。

请改为尝试:

SELECT 
  tblSalaryBand.BandName, tblSalaryBandYear.BandMax, 
  tblSalaryBandYear.BandMin, tblSalaryBand.ID AS SalaryBandID
FROM tblSalaryBand 
LEFT JOIN (
    SELECT * 
    FROM tblSalaryBandYear 
    WHERE ReviewYearID = [Forms]![frmHome]![SelectedYear]
) AS SBY ON tblSalaryBand.ID = SBY.SalaryBandID