仅从一组值中检索给定标识符的多个实体中的行和一个列值

ngynwnxp  于 2021-06-20  发布在  Mysql
关注(0)|答案(5)|浏览(327)

我有一个用于存储audit的数据库表,如下所示(它只是实际表的简单表示,其中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

我需要查找给定学生id和课程id对的所有记录作为标识符,其中状态属于已注册和已完成(即每个已注册和已完成状态有2条记录,或者只有一条已注册或已完成状态的记录)。
注意-对于状态不是已注册和已完成的学生id和课程id,不应存在条目。
输出表-

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

更新-如果我有另一个学生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
ejk8hzay

ejk8hzay1#

对空测试使用左连接

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)
t1rydlwq

t1rydlwq2#

正如您所提到的,您只需要这两个状态-“已注册”和“已完成”,就可以通过子查询实现您的目标

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

sf6xfgos3#

使用distinct和sub查询

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
rvpgvaaj

rvpgvaaj4#

一种解决方案是返回状态为 Enrolled 或者 Completed ,但不是别的:

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...
  )
0x6upsns

0x6upsns5#

我只想排除 STUDENT_ID & COURSE_ID 在哪里 In-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'
                 );

相关问题