Access 2013 - 一对多关系:在 "One" 侧显示所有值,在 "Many" 侧仅显示 select 值

Access 2013 - One to Many relationship: Show all values on "One" side and only select values on "Many" side

我有两个表 - 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]));

有没有一种方法可以显示所有薪资区间,然后仅显示所选审核年份的区间最小值和最大值?

问题是 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