如何查找选课数目最少的学生编号?

zylt 2008-11-25 12:07:17
我想写一个SQL语句,实现从一个表中找到选课数目最少的学生编号。例如:
表名:tab1
字段名:学号 教师号 课程号 学年学期
stu_id,tea_id,cour_id,xnxq_id
...全文
288 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ChinaJiaBing 2008-11-25
  • 打赏
  • 举报
回复

select a.学号 from (select 学号,count(*) 次数 from tab1 group by 学号) a,
(select 学号,min(次数) 次数 from
(select 学号,count(*) 次数 from tab1 group by 学号 ) a
group by 学号) b
a.次数=b.次数
zylt 2008-11-25
  • 打赏
  • 举报
回复
上面的问题实现了,谢谢大家。我还想问,如果想知道学生的姓名的话,这个sql语句怎么写。学生的姓名在info_stu表中。两个表关联怎么办?
等不到来世 2008-11-25
  • 打赏
  • 举报
回复
select top 1 stu_id from 
(
select stu_id,cnt=count(distinct(cour_id))
from tb
group by stu_id
)t
order by cnt
lonlyhawk 2008-11-25
  • 打赏
  • 举报
回复
放在count里

count(distinct 课程号)
等不到来世 2008-11-25
  • 打赏
  • 举报
回复
select top 1 stu_id from tb 
group by stu_id
order by count(distinct cour_id)
zylt 2008-11-25
  • 打赏
  • 举报
回复
课程号有可能是重复的,因为学生可能重修同一门课程。如果想知道他不重复的选课的话,要怎么办?
distinct放在哪?
mathsfan 2008-11-25
  • 打赏
  • 举报
回复
你们都不回答了我就只能顶了...
ljhcy99 2008-11-25
  • 打赏
  • 举报
回复
能查询出 每一个学年 选课数目最少的学生编号
select min(cnt),stu_id,xnxq_id
from
(select count(cour_id) as cnt, stu_id,xnxq_id
from table
group by stu_id,xnxq_id
) AS AA

group by stu_id,xnxq_id

不考虑学年则 去掉xnxq_id
水族杰纶 2008-11-25
  • 打赏
  • 举报
回复
--try
select * from (
select 学号,count(课程号) total
from tab1
group by 学号)a
where total =(select min(total) from (select 学号,count(课程号) total
from tab1
group by 学号)b)
hyde100 2008-11-25
  • 打赏
  • 举报
回复
select top 1 *
from
(select 学号,count(课程号) total
from tab1
group by 学号
)t
order by total
fcuandy 2008-11-25
  • 打赏
  • 举报
回复
select top 1 stu_id from tb group by stu_id order by count(*)

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧