数据:
Programs
- degree (name)
- Course (course list)
Degrees
- code (unique identifier)
- name
- type (either postgrad or undergrad)
Course
- code (unique identifier)
- name
因此,undergrad
和postgrad
学位中都开设了一些课程,我想知道如何获得所有这些课程。
答案 0 :(得分:0)
这应该可以帮助您满足要求,
SELECT C.Name
from
Program P
inner join Degrees D
on D.Code=P.Degree and (D.Type = 'postgrad' OR D.Type = 'undergrad')
inner join Course C
on C.Code=P.Course
group by C.Name
having count(*)>1
答案 1 :(得分:0)
一种方法是聚合:
select p.course
from program p inner join
degrees d
on d.Code = p.Degree
where d.type in ('postgrad', 'undergrad')
group by p.course
having count(distinct d.type) = 2;
仅在需要名称而不是代码时才需要加入course
。
答案 2 :(得分:0)
我会用2个子选择来做到这一点:
SELECT c.name
from course C
-- at least one postgrad programs/degrees for this course
inner join (
select distinct p.course
from Program P inner join Degrees D on d_pg.Code=P.Degree
where D.Type = 'postgrad'
) as P_pg on C.Code=P_pg.Course
-- at least one undergrad programs/degrees for this course
inner join (
select distinct p.course
from Program P inner join Degrees D on d_pg.Code=P.Degree
where D.Type = 'undergrad'
) as P_ug on C.Code=P_ug.Course
where 1=1
;
答案 3 :(得分:0)
我会使用交叉点:
select courses.name
from courses
inner join programs on programs.course = courses.name
inner join degrees on degrees.code = programs.degree
where degrees.type = 'UG'
intersect
select courses.name
from courses
inner join programs on programs.course = courses.name
inner join degrees on degrees.code = programs.degree
where degrees.type = 'PG';
查询如下:选择所有本科课程,并将结果与研究生课程相交。
如果只想要本科课程,可以使用except
而不是相交(交换两个查询,如果只想要研究生课程,则使用except
)。