急!经典学生老师问题

xuanzg 2003-09-27 04:42:07

现有学生表s 字段:s_id, s_name, t_id
老师表t 字段:t_id, t_name, t_other
现在需要用一条语句实现:
查询出所有老师的信息,和他所教的学生个数。
拜托大家了
...全文
111 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
double22822 2003-09-27
  • 打赏
  • 举报
回复
select a.t_id,a.t_name,a.t_other,b.count from t a inner join
(select t.t_id,isnull(count(*),0) as count
from t,s where t.t_id=s.t_id
group by t.t_id) as b
on a.t_id=b.t_id
抄袭,请原谅。
yujohny 2003-09-27
  • 打赏
  • 举报
回复
select t.t_id,max(t.t_name),max(t.t_other),isnull(sum(1),0)
from t left join s on s.t_id=t.t_id
group by t.t_id
DigJim 2003-09-27
  • 打赏
  • 举报
回复
把 join 改成 left join 啊!
xuanzg 2003-09-27
  • 打赏
  • 举报
回复
多谢各位捧场,
可是大家的语句都有一个问题,
那就是可以查询出有学生人数的老师,
但是没有学生的老师查询不到,
正确结果应该 没有学生的老师的学生总和应该显示为0
wzh1215 2003-09-27
  • 打赏
  • 举报
回复
select a.t_id,a.t_name,a.t_other,b.count from t a inner join
(select t.t_id,count(*) as count
from t,s where t.t_id=s.t_id
group by t.t_id) as b
on a.t_id=b.t_id

wzh1215 2003-09-27
  • 打赏
  • 举报
回复
select a.t_id,a.t_name,a.t_other,b.count from t a inner join
(select t.t_id,count(*) as count
from t,s where t.t_id=s.t_id
group by t.t_id) as b
aierong 2003-09-27
  • 打赏
  • 举报
回复


create table 学生表s (s_id int, s_name char(10), t_id int)
create table 老师表t (t_id int, t_name char(10), t_other char(10))

select t_id,t_name,t_other,count(t.*)
from 老师表t as t join 学生表s as s
on t.t_id=s.t_id
zosky 2003-09-27
  • 打赏
  • 举报
回复
select a.*,count(b.*) from s a,t b where a.t_id=b.t_id
yujohny 2003-09-27
  • 打赏
  • 举报
回复
select t.t_id,max(t.t_name),max(t.t_other),count(*)
from t inner join s on s.t_id=t.t_id
group by t.t_id

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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