首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SQL:如何获得本科和研究生学位的课程?

SQL:如何获得本科和研究生学位的课程?
EN

Stack Overflow用户
提问于 2018-10-29 04:03:13
回答 4查看 147关注 0票数 1

数据:

代码语言:javascript
复制
Programs  
- degree (name)  
- Course (course list)  

Degrees  
- code (unique identifier)  
- name  
- type (either postgrad or undergrad)  

Course  
- code (unique identifier)   
- name  

因此,undergradpostgrad学位都有一些课程,我想知道如何获得所有这些课程。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2018-10-29 11:26:35

一种方法是聚合:

代码语言:javascript
复制
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

票数 0
EN

Stack Overflow用户

发布于 2018-10-29 04:22:26

这会帮助你满足你的要求,

代码语言:javascript
复制
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
票数 0
EN

Stack Overflow用户

发布于 2018-10-29 14:14:09

我会用两个子项来做,像这样:

代码语言:javascript
复制
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
;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53038592

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档