There is a score table SC, the table structure is SC (sid, cid, course), the branch corresponds to the student ID, course ID and student scores. The following test data
sid cid course
1 001 67
1 002 89
1 003 94
2 001 95
2 002 88
2 004 78
3 001 94
3 002 77
3 003 90
Query the ID of students who have studied both the '001' course and the '003' course. The expected result is
1
3
solve:
select a.sid
from (
select sid
,cid
from sc a
where cid = '001'
) a
inner join (
select sid
,cid
from sc a
where cid = '003'
) b
on a.sid = b.sid
;
sid
1
3
备注:建表和数据
create table sc(s_id int,c_id varchar(10),course int);
insert into sc values(1,'001',67);
insert into sc values(1,'002',89);
insert into sc values(1,'003',94);
insert into sc values(2,'001',95);
insert into sc values(2,'002',88);
insert into sc values(2,'004',78);
insert into sc values(3,'001',94);
insert into sc values(3,'002',77);
insert into sc values(3,'003',90);