如何求多个字段的和?

ngyxh 2003-06-07 01:50:52
在一个数据库中求一个字段的和为:
select sum(字段名) as getsum from tablename where 条件
但是求多个字段的和代码应该怎么写呢?
如:
一年级 分数 二年级 分数 三年级 分数
甲 80 甲 70 甲 50
乙 70 乙 60 乙 50
丙 60 丙 90 丙 100

要求出甲的总分数,也就是一年级甲的分数+二年级甲的分数+三年级甲的分数。在SQL中代码怎么写啊。
...全文
73 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
WuChenCan 2003-06-29
  • 打赏
  • 举报
回复
select sum(分数1+分数2+分数3......) as count_score from tablename
where name='甲'
blueshu 2003-06-29
  • 打赏
  • 举报
回复
甲能在几个年级?
在一个数据库中求一个字段的和为:
select sum(字段名) as getsum from tablename where 条件 group by 年级
要group吧
firetoucher 2003-06-29
  • 打赏
  • 举报
回复
加一个表的别名,应该就可以了
select sum(fs) as getsum
from (select sum(一年级分数) as fs from tablename where 条件
union
select sum(二年级分数) from tablename where 条件
union
select sum(三年级分数) from tablename where 条件) a

(代理服务器有问题,不能发长的留言,请原谅)
--------------------------------------
看!
那支正飞向太阳的蛾子.....
就是我!
--------------------------------------
firetoucher 2003-06-29
  • 打赏
  • 举报
回复
加一个表的别名,应该就可以了
select sum(fs) as getsum
from (select sum(一年级分数) as fs from tablename where 条件
union
select sum(二年级分数) from tablename where 条件
union
select sum(三年级分数) from tablename where 条件) a

(代理服务器有问题,不能发长的留言,请原谅)
--------------------------------------
看!
那支正飞向太阳的蛾子.....
就是我!
--------------------------------------
xiguali 2003-06-09
  • 打赏
  • 举报
回复
有这么设计数据库的吗?花点时间设计一下数据库会带来很多好处的!
things 2003-06-09
  • 打赏
  • 举报
回复
select sum(fs) as getsum
from (select sum(一年级分数) as fs from tablename where 条件
union
select sum(二年级分数) from tablename where 条件
union
select sum(三年级分数) from tablename where 条件)

http://expert.csdn.net/Expert/topic/1893/1893992.xml?temp=.5286981
--------------------------------------------------------------------
每一个sum(分数) 都需要一个 as fs
否则无法统计到所有fs, sum(fs)
gegangqiao 2003-06-08
  • 打赏
  • 举报
回复
到现都还不知道你的表结构!
字段名:一年级 分数 二年级 分数 三年级 分数 ???
表根本就没有按三个苑式来做,不于回答!
请先出示优化后的表结构!

pigeonwind 2003-06-08
  • 打赏
  • 举报
回复
select sum(字段名1+字段名2+字段名3......) as getsum from tablename where 条件
group by ..
esu 2003-06-08
  • 打赏
  • 举报
回复
select sum(字段名1+字段名2+字段名3......) as getsum from tablename group by name
zklove 2003-06-07
  • 打赏
  • 举报
回复
同上
jsyhello 2003-06-07
  • 打赏
  • 举报
回复
select sum(fs) as getsum
from (select sum(一年级分数) as fs from tablename where 条件
union
select sum(二年级分数) from tablename where 条件
union
select sum(三年级分数) from tablename where 条件)
ngyxh 2003-06-07
  • 打赏
  • 举报
回复
用ADN还是用OR?

如果用AND 那么如果第二个条件不成立时,第一个条件成立时,也不会相加

如果用OR 第只要有一个条件成立时就相加那么加第二第三个条件有什么用
hammer_shi 2003-06-07
  • 打赏
  • 举报
回复
条件不一样可以加到后面么?这又什么影响?
ngyxh 2003-06-07
  • 打赏
  • 举报
回复
第二和第三个字段的条件呢?第二和第三个字段也要有条件的.并且条件和第一个不同啊.
hammer_shi 2003-06-07
  • 打赏
  • 举报
回复
select sum(字段名1+字段名2+字段名3......) as getsum from tablename where 条件

5,392

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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