CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
IBM Rational 系统开发最佳实践工具包 WebSphere MQ 最佳实践 TOP 15
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  MS-SQL Server >  基础类

两个查询问题,想了几个小时都没有想出来,请大家给点意见

楼主yaowei1188(游戏开发)2006-03-04 17:35:15 在 MS-SQL Server / 基础类 提问

题目一:表A有   学生ID,课程ID,(都为主键)字段  
  表B有   课程ID字段(主键)  
  请问如何查询出   选了所有课程的学生ID呢?  
  题目二:表A中,选的课程最多的学生ID?  
  问题点数:100、回复次数:5Top

1 楼zhaoanle(zhao)回复于 2006-03-04 18:06:25 得分 50

一.  
  select   学生ID   from   表A     group   by   学生ID   having   count(学生ID)=(select   count(*)   from   表B)  
   
  二.  
  select   b.学生ID   from    
  (select   max(a.数量)   as   '数量'   from   (select   学生ID,count(学生ID)   as   '数量'   from   表A   group   by   学生ID)   a)   c,  
  (select   学生ID,count(学生ID)   as   '数量'   from   表A   group   by   学生ID)   b  
  where   c.数量=b.数量Top

2 楼wgsasd311(自强不息)回复于 2006-03-04 19:30:58 得分 30

--ta:学生表(stuid,sname)  
  --tb:选课表(stuid,cno,grade)  
  --tc:课程表(cno,cname  
  --1选了所有课程的学生ID及姓名  
  select   stuid,sname   from   ta   a    
  where   not   exists  
  (select   1   from   tc   c   where   not   exists  
  (select   *   from   tb   b   where   a.stuid=b.stuid   and   c.cno=b.cno   )  
  )  
  --or   或只取学生ID,通过两表  
  select   distinct   stuid  
  from   tb   a  
  where   not   exists  
  (  
  select   1   from   tc   b   where   not   exists  
  (  
  select   *   from   tb   c   where   c.stuid=a.stuid   and   c.cno=b.cno    
  )  
  )  
  --2表A中,选的课程最多的学生ID及姓名  
  select   stuid  
  from   tb   a  
  where    
  not   exists  
  (select   1   from   tb   group   by   stuid    
  having   count(1)>(select   count(1)   from   tb   where   stuid=a.stuid)   )Top

3 楼wgsasd311(自强不息)回复于 2006-03-04 19:34:03 得分 0

--ta:学生表(stuid,sname)  
  --tb:选课表(stuid,cno,grade)  
  --tc:课程表(cno,cname  
  --1选了所有课程的学生ID及姓名  
  select   stuid,sname   from   ta   a    
  where   not   exists  
  (select   1   from   tc   c   where   not   exists  
  (select   *   from   tb   b   where   a.stuid=b.stuid   and   c.cno=b.cno   )  
  )  
  --or   或只取学生ID,通过两表  
   
  select   distinct   stuid  
  from   tb   a  
  where   not   exists  
  (  
  select   1   from   tc   b   where   not   exists  
  (  
  select   *   from   tb   c   where   c.stuid=a.stuid   and   c.cno=b.cno    
  )  
  )  
  --2表A中,选的课程最多的学生ID及姓名  
  select   *   from   ta   a  
  where   not   exists  
  (  
  select   1   from   tb   group   by   stuid    
  having   count(1)>(select   count(1)   from   tb   where   stuid=a.stuid)  
  )  
   
  --or   或只取学生ID,通过两表  
  select   distinct   stuid  
  from   tb   a  
  where    
  not   exists  
  (select   1   from   tb   group   by   stuid    
  having   count(1)>(select   count(1)   from   tb   where   stuid=a.stuid)   )Top

4 楼scmail81(琳·风の狼(修罗))回复于 2006-03-04 20:31:53 得分 0

create   table   A  
  (  
        学生Id   int,  
        课程Id   int  
  )  
   
  insert   A   select   1,1  
  insert   A   select   1,2  
  insert   A   select   1,3  
  insert   A   select   1,4  
  insert   A   select   2,1  
  insert   A   select   2,2  
  insert   A   select   2,3  
  insert   A   select   3,1  
  insert   A   select   3,2  
   
  create   table   B  
  (  
      课程Id   int  
  )  
  insert   B   select   1  
  insert   B   select   2  
  insert   B   select   3  
  insert   B   select   4  
   
  1.  
  select   A.学生Id  
  from  
    (select   学生Id,count(*)   as   num   from   A   group   by   学生Id)   A,  
    (select   count(*)   as   num   from   B)B  
  where   A.num=B.num  
   
  2.  
  select   top   1   WiTh   Ties   T.学生ID   from   A   T   order   by   (select   count(*)   from   A   where   T.学生ID=学生ID)   DESC  
  or  
  select   distinct   *   from   (  
  select   top   1   WiTh   Ties   T.学生ID   from   A   T   order   by   (select   count(*)   from   A   where   T.学生ID=学生ID)   DESC)   TTop

5 楼scmail81(琳·风の狼(修罗))回复于 2006-03-04 20:32:19 得分 20

2.  
  select   top   1   WiTh   Ties   T.学生ID   from   A   T   order   by   (select   count(*)   from   A   where   T.学生ID=学生ID)   DESC  
  --or  
  select   distinct   *   from   (  
  select   top   1   WiTh   Ties   T.学生ID   from   A   T   order   by   (select   count(*)   from   A   where   T.学生ID=学生ID)   DESC)   TTop

相关问题

  • 数据库查询顺序问题,请大伙发表意见。
  • 请教查询
  • Hibernate 按小时查询的问题
  • 请教SQL查询
  • 请教SQL查询
  • 请教SQL查询
  • 查询最基本问题,很简单。半小时后结帐。
  • 查询24小时各时间段的记录
  • 关于数据库模糊查询的问题!请大家多多发表意见!高分相送!来者有分!
  • 我写了一句SQL语句,查询9万条数据,但半小时也不见结果,但在查询1千条数据时却可以,请高手为我指点如何更改执行效率

关键词

  • 学生
  • 课程
  • stuid
  • cno
  • tb
  • 数量
  • 学生id
  • ta
  • tc
  • count

得分解答快速导航

  • 帖主:yaowei1188
  • zhaoanle
  • wgsasd311
  • scmail81

相关链接

  • SQL Server类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo