你好,我有一个问题给你们。您可以在下面的图片中看到注册和学生表。我想运行查询从学生表中获取名字和姓氏,但学生应该属于少于5名注册学生的部分。
这有什么意义吗?如果我不清楚,请问我一个问题。非常感谢任何帮助。
答案 0 :(得分:1)
如果您可以使用分析函数保存一些工作,为什么要多次访问一个表?
select
first_name, last_name
from
(
select
s.first_name, s.last_name, count(*) over(partition by e.section_id) as enrollment_count
from
student s
join enrollment e using (student_id)
)
where
enrollment_count < 5;
答案 1 :(得分:0)
这应该有效。
SELECT s.FIRST_NAME, s.LAST_NAME
FROM student s
WHERE s.STUDENT_ID IN (
SELECT e1.STUDENT_ID
FROM e1.enrollment
WHERE e1.SECTION_ID IN (
SELECT e2.SECTION_ID
FROM e2.enrollment
GROUP BY e2.SECTION_ID HAVING COUNT(DISTINCT e2.STUDENT_ID) < 5
)
)
答案 2 :(得分:0)
select t2.first_name, t2.last_name
from table2 t2
inner join
(select student_id from table1 where section_id in
(select section_id from
(select section_id, count(student_id)
from table1
group by section_id
having count(student_id) < 5
)
)
)t1
on t1.student_id = t2.student_id;
它不漂亮,但它应该有用。