在asp中,我設置了幾個下拉菜單,允許用戶根據選擇來篩選頁面上顯示的數據。SQL優化where條款中的條件
這些下拉列表是數據收集,一年組例如11年,主題例如英語,教學組和一個子組。
數據的採集,年和主題都必須選擇,但教學組和子組可以保留不選(全選),單獨選擇中選出的。
我在哪裏SQL存儲過程的條款是因爲在亞組的許多選項現在巨大。以下是幾個選項,其中沒有選擇子組或者選擇了女孩子組或者選擇了男子子組。除此之外還有很多,但爲了簡潔起見,我限制了我發佈的內容。
我的問題是我可以以任何方式優化此代碼,還是我最簡潔?如果你需要更多的例子,請讓我知道,我會將它們添加到帖子中。
where
--All
(@TeachingGroup = 'Select All' AND ([StuYear] = @StuYear) AND ([DataCollection] = @DataCollection) AND ([Name] = @SubjectName) AND @Subgroup='Select All'
OR
@TeachingGroup <> 'Select All' AND ([StuYear] = @StuYear) AND ([DataCollection] = @DataCollection) AND ([Name] = @SubjectName) AND ([TeachingGroup] = @TeachingGroup) AND @Subgroup='Select All')
or
--Gender Girls
(@TeachingGroup = 'Select All' AND ([StuYear] = @StuYear) AND ([DataCollection] = @DataCollection) AND ([Name] = @SubjectName) AND Gender =
case when @Subgroup = 'GenF' then 'F' end)
OR
@TeachingGroup <> 'Select All' AND ([StuYear] = @StuYear) AND ([DataCollection] = @DataCollection) AND ([Name] = @SubjectName) AND ([TeachingGroup] = @TeachingGroup) AND Gender = case when @Subgroup = 'GenF' then 'F' end
or
--Gender boys
(@TeachingGroup = 'Select All' AND ([StuYear] = @StuYear) AND ([DataCollection] = @DataCollection) AND ([Name] = @SubjectName) AND Gender =
case when @Subgroup = 'GenM' then 'M' end)
OR
@TeachingGroup <> 'Select All' AND ([StuYear] = @StuYear) AND ([DataCollection] = @DataCollection) AND ([Name] = @SubjectName) AND ([TeachingGroup] = @TeachingGroup) AND Gender = case when @Subgroup = 'GenM' then 'M' end
此答案適合我。我也喜歡它有兩個選項供我試用,所以我將在未來考慮使用動態sql方法,但現在標準結果工作正常。 – Matt