求一句组合SQL

8047 2007-01-30 10:26:00
我有两个表,
A表:id,name,
B表:aid,num

a表内容
id name
1 a
2 a
3 a
4 b
5 b
6 c
b表内容
aid num
1 11
2 0
3 2
4 5
5 0
6 2
我要显示的内容为
name allnum
a 13
b 5
c 2
请问这样的组合SQL语句怎么样写,谢谢啦
...全文
118 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
8047 2007-01-30
  • 打赏
  • 举报
回复
谢谢大家
marco08 2007-01-30
  • 打赏
  • 举报
回复
create table A(id int, name varchar(10))
insert A select 1, 'a'
union all select 2, 'a'
union all select 3, 'a'
union all select 4, 'b'
union all select 5, 'b'
union all select 6, 'c'
create table B(aid int, num int)
insert B select 1, 11
union all select 2, 0
union all select 3, 2
union all select 4, 5
union all select 5, 0
union all select 6, 2

select A.name, allnum=sum(B.num) from A
inner join B on A.id=B.aid
group by A.name

--result
name allnum
---------- -----------
a 13
b 5
c 2

(3 row(s) affected)
子陌红尘 2007-01-30
  • 打赏
  • 举报
回复
select a.name,sum(b.num) as allnum from a,b where a.id=b.aid group by a.name
子陌红尘 2007-01-30
  • 打赏
  • 举报
回复
select a.name,sum(b.num) as allnum from a,b where a.id=b.id group by a.name

34,590

社区成员

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

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