写一个查询来显示所有在他们出现的所有科目中获得50多个学生的学生的姓名?

问题描述 投票:0回答:2

我一直在网上学习sql。我对这个问题有些怀疑。这是我的答案,但它没有正常工作。

回答:

select distinct student_name 
  from student s 
  join mark m on s.student_id=m.student_id
 where value > 50 order by student_name;

enter image description here

sql
2个回答
1
投票

您的查询已结束。您只需要检查所有标记是否> 50,而不仅仅是一个。您可以通过student_id分组并使用HAVING子句断言所有标记> 50,这与最小标记> 50相同:

SELECT s.student_name 
FROM student s
JOIN mark m ON s.student_id=m.student_id
GROUP BY s.student_name
HAVING MIN(m.value) > 50
ORDER BY s.student_name 

0
投票

您可以检查所有科目的计数以及学生的科目是否等于50

  select m.student_id, count(*) tot_50_count
  from  mark m 
  where m.value > 50 
  gropup by m.student_id 

  select distinct student_name from student s 
  join (
      select m.student_id, count(*)  tot_count
      from  mark m 
      group by m.student_id 
  ) t1 on t1.student_id = s.student_id 

  join (
    select m.student_id, count(*) tot_50_count
    from  mark m 
    where m.value > 50 
    group by m.student_id 
  ) t2  on t2.student_id = s.student_id  
      and t1.tot_count = t2.tot_50_count 
© www.soinside.com 2019 - 2024. All rights reserved.