在线求一个SQL语句,大家帮帮忙,先谢了

sqllong 2006-04-19 05:38:32
表1
bookid bookcount
------------------------------ -----------
ISBN号 10
测试2 5
ISBN号 7

表2
bookid bookcount
------------------------------ -----------
ISBN号 1
ISBN号 1
ISBN号 2
ISBN号 1
ISBN号 1

我期望结果
表1中bookid所有的数量 减去 表2中对应的bookid所有的数量(可能在表2中没有表1中出现的bookid) 得到如下结果:

bookid bookcount
------------------------------ -----------
ISBN号 11
测试2 5

在线等,大家帮帮忙,谢谢咯
...全文
94 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
sqllong 2006-04-19
  • 打赏
  • 举报
回复
where a.bookid*=b.bookid 那个* 是什么意思呢
请教下
sqllong 2006-04-19
  • 打赏
  • 举报
回复
谢谢了
xeqtr1982 2006-04-19
  • 打赏
  • 举报
回复
declare @t table(bookid varchar(10),bookcount int)
insert into @t select 'ISBN号',10
union all select '测试2',5
union all select 'ISBN号',7

declare @a table(bookid varchar(10),bookcount int)
insert into @a select 'ISBN号',1
union all select 'ISBN号',1
union all select 'ISBN号',2
union all select 'ISBN号',1
union all select 'ISBN号',1

select a.bookid,sum(a.bookcount) as bookcount
from
(
select bookid,bookcount from @t
union all
select bookid,-1*bookcount from @a
)a
group by a.bookid
xeqtr1982 2006-04-19
  • 打赏
  • 举报
回复
declare @t table(bookid varchar(10),bookcount int)
insert into @t select 'ISBN号',10
union all select '测试2',5
union all select 'ISBN号',7

declare @a table(bookid varchar(10),bookcount int)
insert into @a select 'ISBN号',1
union all select 'ISBN号',1
union all select 'ISBN号',2
union all select 'ISBN号',1
union all select 'ISBN号',1



select a.bookid,isnull(a.bookcount,0)-isnull(b.bookcount,0) as bookcount from (select bookid,sum(bookcount) as bookcount from @t group by bookid)a,(select bookid,sum(bookcount) as bookcount from @a group by bookid)b where a.bookid*=b.bookid

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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