mysql inventory数据库_mysql库存数据库中的库存数量视图(Stock Quantity view in mysql inventory database)...

该博客介绍了如何通过子查询在SQL中对相关数据进行分组和求和操作。作者提供了一个示例,展示了如何根据ComponentID将Order_Line和Component_Used表中的数据关联并分别求和,以获取每个组件的净数量。
摘要由CSDN通过智能技术生成

If you just use SUM() function it will sum all the values of the table. If you want to sum only the related values, you have to first group the values by an id and then sum them. That's what the below sub query does.

Here is a model of Table1- Order_Line

QujB0.png

Here is a model of Table2- Component_Used 9f2eY.png

I have filled only necessary details. You can see that there are multiple values of Order_LineQuantity for a given component.

And similarly multiple values exist for Component_UsedQuantity for a given component.

I have linked this using the ComponentID from Component Table.

Mll7E.png

SELECT ComponentID AS ComponentID,(ordertable.sumorder-componenttable.sumcomponent) as quantity

FROM Component,

(SELECT Order_LineComponentID as id,SUM(Order_LineQuantity) as sumorder FROM Order_Line GROUP BY Order_LineComponentID)ordertable,

(SELECT Component_UsedComponentID as id,SUM(Component_UsedQuantity) as sumcomponent FROM Component_Used GROUP BY Component_UsedComponentID)componenttable

WHERE

ComponentID=ordertable.id and ComponentID=componenttable.id

Now when you execute the query, it selects the required details after all the grouping operations.

sWKdl.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值