我陷入了SQL select,它有多个值

时间:2015-01-15 08:06:27

标签: sql ms-access relational-division

Table name: test
id - student - class
1 - Jane - A
2 - David - B
3 - Jane - C
4 - Nick - A
5 - Nick - B
6 - Smith - C
----------------

现在我想找出同时注册A和C类的学生名(必须是A和C) 结果将显示Jane,因为她注册了A和C

p / s:我正在使用MS - ACCESS 2007

4 个答案:

答案 0 :(得分:2)

这是relational division的情况。我们在这里组建了一系列技术:

应该是最快的解决方案之一:

SELECT student
FROM   test t1
JOIN   test t2 USING (student)
WHERE  t1.class = 'A'
AND    t2.class = 'C';

MS Access 具有相当基本的SQL实现。它使用双引号作为字符串(与标准相矛盾):

SELECT test.student
FROM   test
INNER  JOIN test AS t2 ON test.student = t2.student
WHERE  test.class = "A"
AND    t2.class = "C";
仅当组合DISTINCT不唯一时才需要

(student, class)(或结果分组) - 在大多数用例中应该是这样。

答案 1 :(得分:0)

由学生分组,只选择课程AC。然后只带那些有两个条目的学生

select student
from test
group by student
having sum(iif(class = 'A',1,0)) > 0
and sum(iif(class = 'B',1,0)) > 0

答案 2 :(得分:0)

取名A班级学生的姓名& C班级学生&对这些结果集进行Intersection

select distinct
 a.student
from
 test a
 inner join test b on a.id = b.id
 where a.class = 'A' and b.class= 'C'

答案 3 :(得分:0)

这是一种方式:

SELECT DISTINCT STUDENT 
FROM TEST T 
WHERE 
EXISTS (SELECT NULL FROM TEST T1 WHERE T.STUDENT = T1.STUDENT AND T1.CLASS='A') AND
EXISTS (SELECT NULL FROM TEST T2 WHERE T.STUDENT = T2.STUDENT AND T2.CLASS='C');