2014-10-28 110 views
1

我有以下模式的表:不具有特定的值选择IDS

+---------+--------+ 
|studentId | course | 
+---------+--------+ 
|1  | 2  | 
|1  | 3  | 
|1  | 4  | 
|1  | 5  | 
|2  | 4  | 
|2  | 5  | 
+---------+--------+ 

,我想执行一个查询来获取学生证不具有当然2,3

select * from students where course not in (2,3); 

但它返回学生ID 1和2,我希望它只返回学生ID 2。

我该怎么做?

回答

2

你可以做这样的:

select * from students where studentId not in -- exclude all students from course 2 & 3 
(
    --find all the students in course 2 & 3 
    select distinct studentId --could be duplicates might as well grab a distinct list. 
    from students 
    where course in (2,3) 
) 
+0

这是怎么回事? – JanR 2014-10-29 00:49:27

+1

严格逻辑的“没有课程2和3”意味着。然而,在问题中提供的代码示例表明,OP实际上想过滤设置课程2或课程3的学生。 – wolfgangwalther 2014-10-29 01:22:20

+0

是的,这是真的! – Strawberry 2014-10-29 08:35:07

0

这应该工作:

select 
    * 
from 
    students s 
where 
    not exists (select 
        1 
       from 
        students ss 
       where 
        ss.studentID = s.studentID 
        and ss.course in (2,3)); 
+0

为您的子查询将始终在学生表 – JanR 2014-10-29 00:03:55

+1

@JanR良好的渔获每个记录返回1,这是行不通的,我加了条件到WHERE子句 – JRLambert 2014-10-29 00:06:31

+0

你还在做选择1,学生将返回每个记录的值1。 – JanR 2014-10-29 00:08:40

2

这个答案假定OP要筛选出具有某一路线2或课程3位或学生他们设置了。

起初,发现所有的学生,谁当然有2个或3

SELECT DISTINCT studentId 
FROM students 
WHERE course IN (2,3) 

然后,找到所有的学生,谁在该列表中

SELECT * 
FROM students 
WHERE studentId NOT IN (...) 

不是如果你只想要回没有课程的学生名单将*替换为DISTINCT studentId

把那些一起:

SELECT DISTINCT studentId 
FROM students 
WHERE studentId NOT IN (
    SELECT DISTINCT studentId 
    FROM students 
    WHERE course IN (2,3) 
) 
-1

JR的查询将执行MySQL的不良< 5.6,只执行或不是和课程。

试试这个:

SELECT 
    id 
FROM foo AS missingfoo 
LEFT JOIN (
      SELECT id FROM foo AS foo1 
      JOIN foo AS foo2 USING (id) 
      WHERE foo1.course=2 
      AND foo2.course=3 
     ) AS z 
USING (id) WHERE z.id IS NULL GROUP BY id; 
0

我会建议使用NOT IN和写作,拉了谁是疗程服用2或3。每个学生这是一个子查询,如果您正在寻找学生谁不走课程2或3.如果您希望排除正在参加BOTH课程的学生,则需要稍微改动一下。让我知道如果是这样的话。

开始通过编写子查询,这是很容易做到:

SELECT * 
FROM students 
WHERE course = 2 OR course = 3 

然后,您可以再次使用NOT IN操作符从表中选择:

SELECT DISTINCT studentid 
FROM students 
WHERE studentid NOT IN (SELECT studentid 
         FROM students 
         WHERE course = 2 OR course = 3); 

而且it works

+0

@Strawberry它是什么? – AdamMc331 2014-10-29 02:06:45

+0

对不起 - 我后来才意识到这个问题是可以解释的,所以你可能是正确的。以某种方式编辑你的帖子,我会给你你的点回 – Strawberry 2014-10-29 08:33:16

+0

@Strawberry我现在从阅读其他答案了解。是的,在OP能够澄清之前,这一条肯定是可以解释的。 – AdamMc331 2014-10-29 12:28:18

1

使用having筛选出具有疗程2或3

select studentId 
from students 
group by studentId 
having sum(course in (2,3)) = 0 
+0

我认为是对的。 – 2014-10-29 00:53:34

+0

这是正确的:select studentId from student group by studentId having sum(course in(2,3))<2; – Strawberry 2014-10-29 01:21:37

+0

@Strawberry我可以阅读这个问题的任何一种方式,但是如果op只想排除已经参加两门课程的学生,然后更改为<2将会工作 – FuzzyTree 2014-10-29 01:36:42

0
SELECT DISTINCT x.studentid 
      FROM student x 
      LEFT 
      JOIN 
       (SELECT studentid 
        FROM student 
       WHERE course IN(2,3) 
       GROUP 
        BY studentid 
       HAVING COUNT(*) = 2) y 
      ON y.studentid = x.studentid 
      WHERE y.studentid IS NULL; 

学生另一个查询(当然,这是极不可能的表格保存学生和课程会被称为studentenrolment可能是一个更好的标题)