我处于这样一种情况,我必须动态创建一个查询,从而降低我的搜索结果。我需要在查询中动态添加“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') ..
答案 0 :(得分: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)