白日梦想jun 2016-04-26 04:51 采纳率: 100%
浏览 1513
已采纳

SQL 查询问题 group by

student(sno,sname,,sdept)
course(cno,cname,ccredit)
sc(sno,cno,grade)
为什么这样写不行呢?
//查询CS系成绩最高的学生的学号、姓名以及成绩

select student.sno,sname,grade from student,sc where student.sno = sc .sno
and grade=(select max(grade) from sc where sc.sno = sno and sdept = 'cs')
还有一个问题;
//查询每个系的最高成绩
select sdept,max(grade) from sc,student where sc.sno=student.sno group by sdept;这样写把每个系的最高分列出来了,当我想把sno列出来,所以加上了sc.sno,下面的就什么结果也没有,是错的。
--select sdept,sc.sno,max(grade) from sc,student where sc.sno=student.sno group by sdept,sc.sno
那这样的问题的group by到底该怎么写呢?如果我想把学号等也列出来是不是只可以写成相关查询呢?
select x.sno,sdept,grade from student x,sc where x.sno = sc.sno and grade =
(select max(grade) from sc,student where sc.sno=student.sno and sdept=x.sdept) //这个结果又是对的
谢谢指导。

  • 写回答

2条回答

  • danielinbiti 2016-04-26 05:37
    关注
     select student.sno,sname,grade from student,sc where student.sno = sc .sno
    and grade=(select max(grade) from sc where sc.sno = sno and sdept = 'cs')
    这句不对的原因在于,下面子句中,左边的sdept不存在,在sc表中没有
    (select max(grade) from sc where sc.sno = sno and sdept = 'cs')
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。