从多个整数中检索给定标识符的行,并且仅从一组值中检索一个列值

时间:2018-08-27 12:52:32

标签: mysql sql select subquery relational-database

我有一个用于存储审核的数据库表,如下所示(它只是实际表的简化表示,其中STATUS可以具有多个值之一)

ID | STUDENT_ID | COURSE_ID | STATUS
1  |     5      | 12        | Enrolled
2  |     5      | 12        | In-Progress
3  |     2      | 12        | Enrolled
4  |     2      | 12        | Completed
5  |     5      | 12        | Completed
6  |     2      | 14        | Enrolled

我需要找到给定STUDENT_ID和COURSE_ID对的所有记录作为标识符,其中STATUS属于Enrolled&Completed(即,每个Enrolled&Completed状态有2条记录,或者对于Enrolled或Completed状态只有一条记录)。

注意:对于给定的STUDENT_ID和COURSE_ID,不应存在状态为“已注册并完成”的状态的条目。

输出表-

ID | STUDENT_ID | COURSE_ID | STATUS
3  |     2      | 12        | Enrolled
4  |     2      | 12        | Completed
6  |     2      | 14        | Enrolled

更新-如果我还有另一个STUDENT_ID 2条目,其状态为进行中,它仍应返回状态为已注册并完成的课程。

ID | STUDENT_ID | COURSE_ID | STATUS
1  |     5      | 12        | Enrolled
2  |     5      | 12        | In-Progress
3  |     2      | 12        | Enrolled
4  |     2      | 12        | Completed
5  |     5      | 12        | Completed
6  |     2      | 14        | Enrolled
7  |     2      | 14        | In-Progress

输出表-

ID | STUDENT_ID | COURSE_ID | STATUS
3  |     2      | 12        | Enrolled
4  |     2      | 12        | Completed

5 个答案:

答案 0 :(得分:2)

使用带有空测试的左连接

drop table if exists t;
create table t(ID int, STUDENT_ID int, COURSE_ID int, STATUS varchar(20));
insert into t values
(1  ,     5      , 12        , 'Enrolled'),
(2  ,     5      , 12        , 'In-Progress'),
(3  ,     2      , 12        , 'Enrolled'),
(4  ,     2      , 12        , 'Completed'),
(5  ,     5      , 12        , 'Completed'),
(6  ,     2      , 14        , 'Enrolled');


select t.* from t
left join
(select  student_id,course_id,count(*) from t where status not in('enrolled','completed') group by student_id,course_id) s
on t.STUDENT_ID = s.student_id and t.course_id = s.course_id 
where s.student_id is null;

+------+------------+-----------+-----------+
| ID   | STUDENT_ID | COURSE_ID | STATUS    |
+------+------------+-----------+-----------+
|    3 |          2 |        12 | Enrolled  |
|    4 |          2 |        12 | Completed |
|    6 |          2 |        14 | Enrolled  |
+------+------------+-----------+-----------+
3 rows in set (0.00 sec)

答案 1 :(得分:1)

我只会排除发现STUDENT_ID状态的COURSE_IDIn-Progress

select t.*
from table t
where not exists (select 1 
                  from table t1 
                  where t1.student_id = t.student_id and 
                        t1.course_id = t.course_id and t1.status = 'In-Progress'
                 );

答案 2 :(得分:1)

如前所述,您只需要这两个状态-“已注册”,“已完成” 您可以通过子查询实现目标

SELECT t.* 
FROM   students t 
WHERE  student_id NOT IN (SELECT DISTINCT student_id 
                          FROM   students 
                          WHERE  status NOT IN ( 'Enrolled', 'Completed' ));

答案 3 :(得分:0)

使用不同的子查询

 select distinct * from your_table 
    where STUDENT_ID not in 
      (  select STUDENT_ID from your_table 
         where STATUS in ('In-Progress')
      )

http://sqlfiddle.com/#!9/f6d650/1

ID  STUDENT_ID  COURSE_ID   STATUS
3     2         12          Enrolled
4     2         12          Completed
6     2         14          Enrolled

另一种方式

select t1.* from
(
select * from yourtable 

) as t1

left join  
(
select * from yourtable
             where STATUS  in ('In-Progress')
) as t2 on t1.STUDENT_ID=t2.STUDENT_ID
where t2.id is null

http://sqlfiddle.com/#!9/f6d650/7

ID  STUDENT_ID  COURSE_ID   STATUS
3     2            12     Enrolled
4     2            12     Completed
6     2            14     Enrolled

答案 4 :(得分:0)

一种解决方案是返回状态为EnrolledCompleted的(STUDENT_ID,COURSE_ID)对,但不返回其他任何内容:

select
  student_id,
  course_id
from
  students
group by
  student_id,
  course_id
having
  sum(status in ('Enrolled', 'Completed'))>0
  and sum(status not in ('Enrolled','Completed'))=0

然后您可以使用以下方法提取所有列:

select *
from students
where
  (students_id, course_id) in (
    ..the select query above...
  )