這裏是我的關係寫一個先進的過濾系統
用戶希望能夠相應地增加使用下拉菜單中的過濾器和過濾志願者。過濾器來自VolunteerWorkArea
表(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。我很確定有一個簡單的方法來做到這一點。有人可以幫幫我嗎?謝謝!
你必須之間'V','VWAA聯接幾個實例'和'VWA',我建議你將它「封裝」到視圖中以簡化一些事情。 – 2015-02-06 09:01:01