简单的sql语句问题,请指点.

szheto 2005-08-14 12:55:59
3个表分别如下:
in: model,qty
out: model,qty
product: model,name,unit
怎样得到select model,name,unit,sum(in.qty),sum(out.qty),sum(in.qty)-sum(out.qty) from
.......where......?
如果where in.model=out.model and in.model=product.model的话,会丢失记录.
如果用group by的话,name,unit不能出现........请指点!!!
...全文
174 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
octopus1 2005-08-15
  • 打赏
  • 举报
回复
select model,name,unit,inaa.suminqty ,outaa.sumoutqty,(inaa.suminqty - outaa.sumoutqty)
from product
+=
(select sum(in.qty) as suminqty where inaa.model = product.model) inaa
+=
(select sum(out.qty) as sumoutqty where outaa.model = product.model) outaa
flyboy20 2005-08-15
  • 打赏
  • 举报
回复
学习
connection999 2005-08-15
  • 打赏
  • 举报
回复
eprint自定义打印是一个页面套打工具.它直接在IE浏览器中可视化的设计各种复杂的打印模版,
能够解决在IE浏览器中打印各种复杂的中国式报表及票据。详见: http://218.30.103.110:8080/eprint/index.htm


方成eform自定义表单平台,纯BS结构.100%开放源码.可以在 http://218.30.103.110:8080/eform/index.html 在线试用.
skyfarwolf 2005-08-14
  • 打赏
  • 举报
回复

应该是楼主原来的语句加上我后面的这条语句
skyfarwolf 2005-08-14
  • 打赏
  • 举报
回复

我也遇到了这个问题.
在in 和 out 的相同model记录有多条时会产生 in 和 out 的cross join 效果

我把上面语句 left join 改成 right join
再加上
union
(
select ...... from in,out,product where
product.model not in (select model from in)
or
product.model not in (select model from out)
and ......
group by ......
)
jimu8130 2005-08-14
  • 打赏
  • 举报
回复
复合查询
hedonister 2005-08-14
  • 打赏
  • 举报
回复
用left join 就不会了
szheto 2005-08-14
  • 打赏
  • 举报
回复
因为当同一个model,只有in而没有out的话,就丢失了.
szheto 2005-08-14
  • 打赏
  • 举报
回复
楼上的语句会丢失记录的.
qqqdong 2005-08-14
  • 打赏
  • 举报
回复
select product.model,product.name,product.unit,sum(in.qty),sum(out.qty),sum(in.qty)-sum(out.qty)
from product,in,out where product.model=in.model and product.model=out.model
group by product.model,product.name,product.unit
hedonister 2005-08-14
  • 打赏
  • 举报
回复
上面的语句可能有误,但具体思路就是这样的,楼主自己改改,不行给我短消息
hedonister 2005-08-14
  • 打赏
  • 举报
回复
select model,name,unit,inaa.suminqty ,outaa.sumoutqty,(inaa.suminqty - outaa.sumoutqty)
from product
left join
(select sum(in.qty) as suminqty where inaa.model = product.model) inaa
left join
(select sum(out.qty) as sumoutqty where outaa.model = product.model) outaa

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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