我在sql中有一个表“Student”,结构为:
StudentId FirstName LastName
1 X Y
....
AND语言表,其结构:
LanguageId Name
1 English
2 Mandarin
3 Spanish
.....
和关系表StudentLanguage(学生说的语言)
StudentId LanguageId
1 1
1 3
2 1
2 2
从我的asp.net页面,我想使用复选框按口语过滤学生。
例如,当我检查英语时,Madarin我想让学生说英语和Madarin 当我检查法语,西班牙语,英语==>让学生说法语,英语和西班牙语。要做到这一点,我将一个名为@LanguageTable(LanguageId smallint)
的语言参数表传递给存储过程。
如何在不使用光标的情况下使用此表来获取学生。
我尝试过使用CTE但没有结果。
答案 0 :(得分:1)
SELECT s.StudentId, s.FirstName, s.LastName
FROM Student s
WHERE NOT EXISTS(SELECT *
FROM @LanguageTable l
WHERE NOT EXISTS(SELECT *
FROM StudentLanguage sl
WHERE sl.LanguageId = l.LanguageId
AND sl.StudentId = s.StudentId))
答案 1 :(得分:1)
我没有测试过这个,但是:
SELECT s.StudentId, s.FirstName, s.LastName
FROM Student s
INNER JOIN StudentLanguage sl ON sl.StudentId = s.StudentId
INNER JOIN @Language l ON l.LanguageId = sl.LanguageId
GROUP BY s.StudentId, s.FirstName, s.LastName
HAVING Count(*) = (SELECT COUNT(*) FROM @Language)
答案 2 :(得分:1)
试试这个:
select s.StudentId,s.FirstName,s.LastName
from Stundent s
join StudentLanguage sl on s.StudendID=sl.StudentId
join @LanguageTable on sl.LanguageId=@LanguageTable.LanguageId
group by s.StudentId,s.FirstName,s.LastName
having count(*)=(select count(*) from @LanguageTable)