-1

我处于一种情况,我必须动态创建一个查询来缩小我的搜索结果。我需要根据用户从主题/标准复选框中的选择动态地在查询中添加“AND”条件。

SELECT First_Name, Last_Name, City, Subject_1, Subject_2, Subject_3, 
       Standard_From, Standard_To FROM TutorProfile WHERE City = 'Mumbai'  AND (Subject_1 = 'Maths' OR Subject_2 = 'Maths' OR Subject_3
 = 'Maths') AND (Subject_1 = 'Physics' OR Subject_2 = 'Physics' OR Subject_3 = 'Physics') .. AND (Standar_From = 'Class XII') ..
4

1 回答 1

0

尝试这样的事情:

DECLARE @SQL NVARCHAR(500)
DECLARE @SubjectCommand VARCHAR(50)

SET @SubjectCommand = ''

IF (SubjectIsSelected)
BEGIN
    SET  @SubjectCommand = 'AND (Subject_1 = ''Maths'' OR Subject_2 = ''Maths'' OR Subject_3 = ''Maths'')'
END

SET @SQL = 'SELECT First_Name, Last_Name, City, Subject_1, Subject_2, Subject_3, 
   Standard_From, Standard_To FROM TutorProfile WHERE City = ''Mumbai''' +@SubjectCommand 

EXEC(@SQL)
于 2013-06-14T15:48:12.320 回答