查找产品的最新发货(sql 子选择?)
我有三个表
shipment (shipment_id, shipping_date)
company_order (company_order_id, shipment_id, company_id)
company_order_item (company_order_item_id, company_order_id, product_id)
几家公司聚集在一起并汇总来自单个制造商的订单。该聚合订单称为“发货”。公司在每次发货中都会订购精选的产品:因此并非所有产品都会出现在任何一次发货中或为任何一家公司提供。
如何编写 SQL 查询来查找每个 Product_id 的最新发货?
我看过 SQL 查询 - 获取最新修订(更简单的情况)。
I have three tables
shipment (shipment_id, shipping_date)
company_order (company_order_id, shipment_id, company_id)
company_order_item (company_order_item_id, company_order_id, product_id)
Several companies get together and aggregate orders from a single manufacturer. This aggregate order is called a "shipment". Companies order a selection of products in each shipment: so not all products will be present in any one shipment or for any one company.
How do I write an SQL query find the most recent shipment for each product_id ?
I've looked at
SQL Query - Get Most Recent Revision (much simpler case).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要获取每个产品 ID 的最大发货日期,然后检索发货详细信息,
例如
You need to get the max shipment date per product id and then retrieve the shipment detaisl
Something like
用于说明的 SQL 代码 - (这是 T-SQL,对 SQL Server 友好,但我手边没有任何 mysql。最后一个查询应该进行微小的修改(以适合您的表名称),在 MySQL 中也能很好地工作。
我的逻辑就是找到每个product_id的最新company_order。一旦我有了它,我就可以将company_order_id加入到company_order,并且我有每个product_id的每个最新company_order的shipment_id。
SQL Code to illustrate - (This is T-SQL and is SQL Server friendly, but i didn't have any mysql handy. The last query should with tiny modifications (to suit your table names) work nicely in MySQL as well.
My logic is to find the most recent company_order for each product_id. Once i have that i can just join the company_order_id to company_order, and i have the shipment_id for each most-recent company_order per product_id