我有两个表格 - tblSalaryBand
和tblSalaryBandYear
由tblSalaryBand.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的值?
答案 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