使用LEFT JOIN和UNION组合多个表

时间:2016-07-16 19:03:05

标签: mysql join

我有 5个不同的表来获取数据。但首先我使用3个表进行测试。使用 LEFT JOIN UNION 。到目前为止,这是我们一直在使用的代码。

SELECT sts.TransferDate as 'Transaction Date', sts.TransNumber as 'Document Number', orderset_details.Quantity as 'Quantity', '' as 'Po Number', '' as 'Sales Invoice', orderset_details.Cost as 'Cost', orderset_details.SerialNumber as 'Serial Number', orderset_details.LotNumber as 'Lot Number', location.Location as 'Location' 
FROM orderset_details 
LEFT JOIN sts ON sts.OrdersetId = orderset_details.OrdersetId
LEFT JOIN location ON location.LocationId = sts.LocationIdTo  
WHERE orderset_details.ItemId = '3071' 
UNION 
SELECT drs.OrderDate as 'Transaction Date', drs.TransNumber as 'Document Number', orderset_details.Quantity, drs.PONumber as 'Po Number', '' as 'Sales Invoice', orderset_details.Cost as 'Cost', orderset_details.SerialNumber as 'Serial Number', orderset_details.LotNumber as 'Lot Number', suppliers.Supplier as 'Location' 
FROM orderset_details 
LEFT JOIN drs ON drs.OrdersetId = orderset_details.OrdersetId 
LEFT JOIN suppliers ON suppliers.SupplierId = drs.SupplierId 
WHERE orderset_details.ItemId = '3071'
UNION 
SELECT stockadjustment.TransactionDate as 'Transaction Date', stockadjustment.TransactionId as 'Document Number', orderset_details.Quantity as 'Quantity', '' as 'Po Number', '' as 'Sales Invoice', orderset_details.Cost as 'Cost', orderset_details.SerialNumber as 'Serial Number', orderset_details.LotNumber as 'Lot Number', location.Location as 'Location' 
FROM orderset_details 
LEFT JOIN stockadjustment ON stockadjustment.OrdersetId = orderset_details.OrdersetId 
LEFT JOIN location ON location.LocationId = stockadjustment.LocationId 
WHERE orderset_details.ItemId = '3071' 
Group By orderset_details.ItemId

这是输出。 enter image description here

输出应该只有1,那是最后一行。我的问题是当我的1或2个查询没有重复某些不正确的列时。

我怎样才能显示已经等于特定ItemId的数据?我的查询出了什么问题?

1 个答案:

答案 0 :(得分:2)

在您的查询中,您应该仅在第一个选择中使用别名,并且您没有聚合函数,因此您不需要group by ..因为unio仅返回不同的结果

SELECT 
    sts.TransferDate as 'Transaction Date'
  , sts.TransNumber as 'Document Number'
  , orderset_details.Quantity as 'Quantity'
  , '' as 'Po Number'
  , '' as 'Sales Invoice'
  , orderset_details.Cost as 'Cost'
  , orderset_details.SerialNumber as 'Serial Number'
  , orderset_details.LotNumber as 'Lot Number'
  , location.Location as 'Location' 
FROM orderset_details 
LEFT JOIN sts ON sts.OrdersetId = orderset_details.OrdersetId
LEFT JOIN location ON location.LocationId = sts.LocationIdTo  
WHERE orderset_details.ItemId = '3071' 
UNION 
SELECT 
      drs.OrderDate 
    , drs.TransNumber 
    , orderset_details.Quantity
    , drs.PONumber 
    , ''
    , orderset_details.Cost 
    , orderset_details.SerialNumber 
    , orderset_details.LotNumber 
    , suppliers.Supplier 
FROM orderset_details 
LEFT JOIN drs ON drs.OrdersetId = orderset_details.OrdersetId 
LEFT JOIN suppliers ON suppliers.SupplierId = drs.SupplierId 
WHERE orderset_details.ItemId = '3071'
UNION 
SELECT 
      stockadjustment.TransactionDate 
    , stockadjustment.TransactionId 
    , orderset_details.Quantity 
    , '' 
    , '' 
    , orderset_details.Cost 
    , orderset_details.SerialNumber 
    , orderset_details.LotNumber 
    , location.Location     
FROM orderset_details 
LEFT JOIN stockadjustment ON stockadjustment.OrdersetId = orderset_details.OrdersetId 
LEFT JOIN location ON location.LocationId = stockadjustment.LocationId 
WHERE orderset_details.ItemId = '3071'