[紧急]4张表关联,使用临时表怎样写存储过程?

[复制链接]
查看11 | 回复4 | 2007-10-20 08:38:44 | 显示全部楼层 |阅读模式
tb1中 name:班级名称 , lb:班级类别
tb2中 bh:班机编号 , lb:班级类别
tb3中 bh:班机编号 , id :学号
tb4 中 store:考试成绩,id :学号
想要得出每个班级的总分:用如下的sql语句实现不了:
select name,sum(store)
from tb1 a, tb2 b, tb3 c,tb4 d
where a.lb=b.lband b.bh=c.bh and c.id=d.id
group by name
这样写的话会出现很多重复的记录,得不到真正的store。
因此考虑使用临时表来实现,是不是要生成2个临时表啊??
#temp1 name ,lb,bh
#temp2 bh,id,store
然后 temp1.bh=temp2.bh得到 name 和sum(store)

请问如何来写这个存储过程啊?
回复

使用道具 举报

千问 | 2007-10-20 08:38:44 | 显示全部楼层
在线等,感谢大家踊跃发言。
回复

使用道具 举报

千问 | 2007-10-20 08:38:44 | 显示全部楼层
用左链接嘛
select * FROM tb1 left outer join tb2 on tb1 .lb=tbl2.lb left outer join tb3
on tb3.bh=tb2.bh left outer join tb4 on tb4.id = tb3.id
回复

使用道具 举报

千问 | 2007-10-20 08:38:44 | 显示全部楼层
这表设计的有问题
回复

使用道具 举报

千问 | 2007-10-20 08:38:44 | 显示全部楼层
感谢cosio,问题解决!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行