如何查询2个表的数据---Sql语句的问题

cqbonny 2006-11-15 08:15:49
我有2个表A,B
A
------
id A_count
1 10
1 10
2 20
2 20
B
------
id B_count
1 10
2 15
------
我的想法是用一条sql语句实现这样的功能,如下表
------
id a_count b_count
1 20 10
2 40 15
------
我的意思就是这样,但是我使用join,然后sum,始终不对,请教一下大家。
谢谢,先给100分,不够再给
...全文
109 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
cqbonny 2006-11-15
  • 打赏
  • 举报
回复
47522341(睡到8:30
是错误的,你可以试试
liangpei2008 2006-11-15
  • 打赏
  • 举报
回复
我的意思就是这样,但是我使用join,然后sum,始终不对,请教一下大家。
--------------
连接问题.
对于这种情况,一般是先汇总后再与第二张表相连,更清晰一些!
47522341 2006-11-15
  • 打赏
  • 举报
回复
select a.id , sum(a.a_count) as a_count, sum(b.b_count) as b_count
from a left join b on a.id = b.id
groubp by a.id
liangpei2008 2006-11-15
  • 打赏
  • 举报
回复
在SQL版已经为您答过了!
http://community.csdn.net/Expert/topic/5160/5160963.xml?temp=.4844477
liangpei2008 2006-11-15
  • 打赏
  • 举报
回复
--Table1
Declare @t table(id int,A_count int)
insert @t select 1 ,10
union all select 1,10
union all select 2,20
union all select 2,20
-----table2
declare @t1 table(id int,B_count int)
insert @t1 select 1,10
union all select 2,15
--SQL
select A.id,A.a_count,B.B_count from
(
select id,sum(a_count) as A_count
from @t group by id) A
inner join @t1 B
on A.id=B.id

2,497

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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