使用不同的列从mysql不同的表和不同的ID中检索数据

时间:2017-04-30 16:57:28

标签: mysql

我有这三张桌子......

   currency (Currency), 
   purchasingqty (PurchasingID, Rate, Qty)
   salesqty (SalesID, Rate, Qty)

我只需要在一个查询中从这3个表中检索记录,而不需要重复。但是这个会返回重复项。

SELECT
          currency.Currency,
          purchASingqty.PurchASingID,
          IFNULL(purchASingqty.Rate,0) AS Pur_Rate,
          SUM(IFNULL(purchASingqty.Qty,0)) AS Pur_Qty,
          salesqty.SalesID,
          IFNULL(salesqty.Rate,0) AS Sales_Rate,
          SUM(IFNULL(salesqty.Qty,0)) AS Sales_Qty
FROM     currency
LEFT JOIN salesqty ON currency.Currency = salesqty.Currency
LEFT JOIN purchASingqty ON currency.Currency =  purchASingqty.Currency
GROUP BY
          purchASingqty.PurchASingID,
          salesqty.SalesID

0 个答案:

没有答案