PHP Mysql连接和Where子句

时间:2017-06-18 22:46:01

标签: php sql left-join

我在下面的陈述中需要帮助。我需要放置WHERE items.IID = 8,以便仅显示与IID编号8相关的详细信息。但是当我使用WHERE items.IID = 8时,它无效。我必须使用这种类型的连接,因为我想做一些字段的Sum和Count。有3个表。第一个是items,第二个是ItemPurchaseHistory,第三个是ItemIssuedHistory

SELECT items.IID, items.ItemName, ItemPurchaseHistorySum.SumOfUnitsPurchased, ItemPurchaseHistorySum.SumOfCost,
ItemPurchaseHistoryCount.CountOfUnitsPurchased,
ItemIssuedHistorySum.SumOfUnitsIssued
FROM items

LEFT JOIN (SELECT IID, SUM(UnitsPurchased) AS SumOfUnitsPurchased, SUM(Cost) AS SumOfCost
FROM ItemPurchaseHistory
GROUP BY IID) ItemPurchaseHistorySum ON ItemPurchaseHistorySum.IID = items.IID

LEFT JOIN (SELECT IID, Count(UnitsPurchased) AS CountOfUnitsPurchased
FROM ItemPurchaseHistory
GROUP BY IID) ItemPurchaseHistoryCount ON ItemPurchaseHistoryCount.IID = items.IID

LEFT JOIN (SELECT IID, SUM(UnitsIssued) AS SumOfUnitsIssued
FROM ItemIssuedHistory
GROUP BY IID) ItemIssuedHistorySum ON ItemIssuedHistorySum.IID = items.IID

WHERE item.IID = $ _GET ['id']

ORDER BY items.IID ASC

0 个答案:

没有答案