从3个不同的查询创建一个结果集

时间:2016-09-26 20:06:41

标签: mysql sql

我正在尝试使用三个查询创建结果集。我有三个表库存表,表单订单表和表单订单明细表。我需要能够输入日期范围并获取订购的表单数量,当前库存中有多少表单,以及根据它们是否包含销毁日期销毁的数量。最终,我想要一个结果集,显示:

 InventoryId, FormDescription, Product, Ordered, Shipped, Destroyed, Total ending

使用这些查询获取结果集的最佳方法是什么?

这是我的三个查询

SELECT FOD.InventoryId, SUM(FOD.FormOrderAmount) as totalOrdered,  FOD.FormShippedAmount  FROM tblFormOrder FMO 
JOIN tblFormOrderDetails FOD ON FOD.FormOrderId = FMO.FormOrderId 
WHERE FMO.OrderDateTime BETWEEN '20110101' and '20120101' 
AND FMO.OrderStatus IN ('S')
GROUP BY FOD.InventoryId, FOD.FormShippedAmount -- total shipped by date and inventoryid



SELECT INV.InventoryId, SUM(INV.CurrentAmount) as currentAmount, SUM(INV.OrderAmount) as OrderAmount, 
(SUM(INV.OrderAmount) - SUM(INV.CurrentAmount)) as InventoryUsed
 FROM tblInventory INV
where INV.CreatedOn 
BETWEEN '20110101' and '20120101' 
GROUP BY INV.InventoryId -- current amount based off ordered and used



 select INV.InventoryId, count(*) as total
,  FMO.OrderDateTime as OrderDate, Inv.FormNo, INV.FormDescription, INV.Product 
 from [tblinventory] INV
join tblformorderdetails FOD ON FOD.InventoryId = inv.InventoryId 
join tblformorder FMO on FMO.FormOrderId = FOD.FormOrderId
where INV.DestructionDate  
BETWEEN '20110101' and '20120101' 
group by  
FMO.OrderDateTime,
 Inv.FormNo, INV.FormDescription, INV.Product, INV.InventoryId -- using count to find how many destroyed if they have a destruction date

2 个答案:

答案 0 :(得分:2)

如果你想要一个内部联接,你可以这样做

SELECT *
FROM (...) AS Q1, (...) AS Q2, (...) AS Q3
WHERE Q1.InventoryID = Q2.InventoryID AND Q2.InventoryID = Q3.InventoryID

这只会为您提供所有3个查询中的InventoryID。

您可能不希望这样,您可能想要所有库存ID ......所以你这样做。

SELECT *
FROM (SELECT DISTINCT InventoryID FROM tblFormOrderDetails 
      UNION ALL 
      SELECT DISTINCT InventoryID FROM tblInventory) I
LEFT JOIN (...) Q1 ON I.InventoryID = Q1.InventoryId
LEFT JOIN (...) Q2 ON I.InventoryID = Q2.InventoryId
LEFT JOIN (...) Q3 ON I.InventoryID = Q3.InventoryId

答案 1 :(得分:1)

使每个查询成为您加入的子查询。

SELECT *
FROM (
    SELECT FOD.InventoryId, SUM(FOD.FormOrderAmount) as totalOrdered,  FOD.FormShippedAmount  FROM tblFormOrder FMO 
    JOIN tblFormOrderDetails FOD ON FOD.FormOrderId = FMO.FormOrderId 
    WHERE FMO.OrderDateTime BETWEEN '20110101' and '20120101' 
    AND FMO.OrderStatus IN ('S')
    GROUP BY FOD.InventoryId, FOD.FormShippedAmount -- total shipped by date and inventoryid
) AS q1
LEFT JOIN (
    SELECT INV.InventoryId, SUM(INV.CurrentAmount) as currentAmount, SUM(INV.OrderAmount) as OrderAmount, 
    (SUM(INV.OrderAmount) - SUM(INV.CurrentAmount)) as InventoryUsed
     FROM tblInventory INV
    where INV.CreatedOn 
    BETWEEN '20110101' and '20120101' 
    GROUP BY INV.InventoryId -- current amount based off ordered and used
 ) AS q2 ON q1.InventoryId = q2.InventoryId
LEFT JOIN (
    select INV.InventoryId, count(*) as total
    ,  FMO.OrderDateTime as OrderDate, Inv.FormNo, INV.FormDescription, INV.Product 
     from [tblinventory] INV
    join tblformorderdetails FOD ON FOD.InventoryId = inv.InventoryId 
    join tblformorder FMO on FMO.FormOrderId = FOD.FormOrderId
    where INV.DestructionDate  
    BETWEEN '20110101' and '20120101' 
    group by  
    FMO.OrderDateTime,
     Inv.FormNo, INV.FormDescription, INV.Product, INV.InventoryId -- using count to find how many destroyed if they have a destruction date
) AS q3 ON q1.InventoryId = q3.InventoryId