联合查询

灿烂阳光168 2011-05-13 03:55:10
共四个表
订单主表order(sno(流水号),no(单号))

订单子表orders(sno,no,pno(料号))

进货主表in(sno,no,status(表的状态,如验收,进货))

进货子表ins(sno,no,pno,qty(数量))

现在需要列出订单表中所有料号和每个料号在“验收”状态的数量。

请问SQL要怎么写?谢谢!

...全文
91 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
--小F-- 2011-05-13
  • 打赏
  • 举报
回复
select
b.pno,
sum(case c.status when '验收' then 1 else 0 end) as 数量
from
order a,订单子表 b,in c,进货子表 d
where
a.no=b.no
and
c.no=d.no
and
b.pno=d.pno
and
a.sno=c.sno
group by
b.pno
--小F-- 2011-05-13
  • 打赏
  • 举报
回复
select
b.pno,
sum(case c.status when '验收' then 1 else 0 end) as 数量
from
order a,订单子表 b,in c,进货子表 d
where
a.no=b.no
and
c.no=d.no
and
b.pno=d.pno
and
a.no=c.no
group by
b.pno
灿烂阳光168 2011-05-13
  • 打赏
  • 举报
回复
举例如下:
order
sno no
1 001

orders
sno no pno
1 001 wangwang

in
sno no status
1 000001 验收

ins
sno no pno qty
1 00001 wangwang 1000

查询要得到的结果:
pno qty
wangwang 1000

灿烂阳光168 2011-05-13
  • 打赏
  • 举报
回复
sno是每个表的流水号。
no是每张单的单号。
yibey 2011-05-13
  • 打赏
  • 举报
回复
怎么每个表里都有SNO 和NO

22,209

社区成员

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

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