编写高级过滤系统
Writing an advanced filtering system
这是我的关系
用户希望能够使用下拉菜单添加过滤器并相应地过滤志愿者。过滤器来自 VolunteerWorkArea
table (VolunteerWorkAreaId
)。我这样做的方式很痛苦,可能是因为缺乏知识。每次用户添加新过滤器时,我都必须以复杂的方式更改 SQL。例如,这是用户添加三个过滤器后的 SQL。
SELECT DISTINCT
Volunteer.VolunteerId AS Volunteer_VolunteerId ,
Volunteer.VolunteerFirstName ,
Volunteer.VolunteerLastName ,
Volunteer.VolunteerOtherName ,
Volunteer.VolunteerStreetAddress ,
Volunteer.VolunteerSuburb ,
Volunteer.VolunteerPostCode ,
Volunteer.VolunteerHomePhone ,
Volunteer.VolunteerMobilePhone ,
Volunteer.VolunteerGender
FROM Volunteer
INNER JOIN ( VolunteerWorkArea
INNER JOIN VolunteerWorkAreaAllocation ON VolunteerWorkArea.VolunteerWorkAreaId = VolunteerWorkAreaAllocation.VolunteerWorkAreaId
) ON Volunteer.VolunteerId = VolunteerWorkAreaAllocation.VolunteerId
WHERE VolunteerWorkAreaAllocation.[VolunteerWorkAreaId] = 17
AND Volunteer.VolunteerId IN (
SELECT DISTINCT
Volunteer.VolunteerId
FROM Volunteer
INNER JOIN ( VolunteerWorkArea
INNER JOIN VolunteerWorkAreaAllocation ON VolunteerWorkArea.[VolunteerWorkAreaId] = VolunteerWorkAreaAllocation.[VolunteerWorkAreaId]
) ON Volunteer.[VolunteerId] = VolunteerWorkAreaAllocation.[VolunteerId]
WHERE VolunteerWorkAreaAllocation.[VolunteerWorkAreaId] = 16
AND Volunteer.VolunteerId IN (
SELECT DISTINCT
Volunteer.VolunteerId AS Volunteer_VolunteerId
FROM Volunteer
INNER JOIN ( VolunteerWorkArea
INNER
JOIN VolunteerWorkAreaAllocation ON VolunteerWorkArea.[VolunteerWorkAreaId] = VolunteerWorkAreaAllocation.[VolunteerWorkAreaId]
) ON Volunteer.[VolunteerId] = VolunteerWorkAreaAllocation.[VolunteerId]
WHERE VolunteerWorkAreaAllocation.[VolunteerWorkAreaId] = 15 ) );
所以每次用户添加过滤器时,我都必须通过代码大量更改 SQL。我很确定有一种简单的方法可以做到这一点。有人可以帮帮我吗?谢谢!
如果您希望用户根据 volunteerWorkArea 决定 volunteers,那么更简单的解决方案是使用子表单。
- 创建一个不基于任何 table
的新表单
- 使用向导创建并绑定带有 volunteerWorkArea table 的组合框
(比如 name: combo2)。
- 创建一个子表单并将其与其他两个 table 绑定,即 volunteer &
volunteerWorkAreaAllocation(图像的字段数较少
示例)
- 通过将可见 属性 设置为 false 来隐藏 ID 字段。
- 如图所示设置子窗体控件属性 (combo2 as master
field 和 Child 作为 volunteerWorkAreaID)
这是我的关系
用户希望能够使用下拉菜单添加过滤器并相应地过滤志愿者。过滤器来自 VolunteerWorkArea
table (VolunteerWorkAreaId
)。我这样做的方式很痛苦,可能是因为缺乏知识。每次用户添加新过滤器时,我都必须以复杂的方式更改 SQL。例如,这是用户添加三个过滤器后的 SQL。
SELECT DISTINCT
Volunteer.VolunteerId AS Volunteer_VolunteerId ,
Volunteer.VolunteerFirstName ,
Volunteer.VolunteerLastName ,
Volunteer.VolunteerOtherName ,
Volunteer.VolunteerStreetAddress ,
Volunteer.VolunteerSuburb ,
Volunteer.VolunteerPostCode ,
Volunteer.VolunteerHomePhone ,
Volunteer.VolunteerMobilePhone ,
Volunteer.VolunteerGender
FROM Volunteer
INNER JOIN ( VolunteerWorkArea
INNER JOIN VolunteerWorkAreaAllocation ON VolunteerWorkArea.VolunteerWorkAreaId = VolunteerWorkAreaAllocation.VolunteerWorkAreaId
) ON Volunteer.VolunteerId = VolunteerWorkAreaAllocation.VolunteerId
WHERE VolunteerWorkAreaAllocation.[VolunteerWorkAreaId] = 17
AND Volunteer.VolunteerId IN (
SELECT DISTINCT
Volunteer.VolunteerId
FROM Volunteer
INNER JOIN ( VolunteerWorkArea
INNER JOIN VolunteerWorkAreaAllocation ON VolunteerWorkArea.[VolunteerWorkAreaId] = VolunteerWorkAreaAllocation.[VolunteerWorkAreaId]
) ON Volunteer.[VolunteerId] = VolunteerWorkAreaAllocation.[VolunteerId]
WHERE VolunteerWorkAreaAllocation.[VolunteerWorkAreaId] = 16
AND Volunteer.VolunteerId IN (
SELECT DISTINCT
Volunteer.VolunteerId AS Volunteer_VolunteerId
FROM Volunteer
INNER JOIN ( VolunteerWorkArea
INNER
JOIN VolunteerWorkAreaAllocation ON VolunteerWorkArea.[VolunteerWorkAreaId] = VolunteerWorkAreaAllocation.[VolunteerWorkAreaId]
) ON Volunteer.[VolunteerId] = VolunteerWorkAreaAllocation.[VolunteerId]
WHERE VolunteerWorkAreaAllocation.[VolunteerWorkAreaId] = 15 ) );
所以每次用户添加过滤器时,我都必须通过代码大量更改 SQL。我很确定有一种简单的方法可以做到这一点。有人可以帮帮我吗?谢谢!
如果您希望用户根据 volunteerWorkArea 决定 volunteers,那么更简单的解决方案是使用子表单。
- 创建一个不基于任何 table 的新表单
- 使用向导创建并绑定带有 volunteerWorkArea table 的组合框 (比如 name: combo2)。
- 创建一个子表单并将其与其他两个 table 绑定,即 volunteer & volunteerWorkAreaAllocation(图像的字段数较少 示例)
- 通过将可见 属性 设置为 false 来隐藏 ID 字段。
- 如图所示设置子窗体控件属性 (combo2 as master field 和 Child 作为 volunteerWorkAreaID)