sql语句如何把查询结果中某一字段相同的列的另一字段值相加 应该怎么写

2024-12-01 10:23:13
推荐回答(2个)
回答1:

假设表table有字段a,b,c,现在要把a相同的,b相加,假设b是int类型,语句:
select
sum(b)
from
table
where
a
in
(
select
a
from
table
group
by
a
having
count(a)
>
1
)
and
sum(b)
<
某个值
group
by
a
不显示的在语句再加判断条件就好了

回答2:

你的意思是表tb中有 b,c两列?然后查询tb,若b中的值相同,则把相同的b值,对应的c字段的值加起来?!
select b,sum(c) from tb group by b若要在过滤
sum(c)<=某值的,
select b,sum(c) from tb group by b having sum(c)<=某值