2014-03-27 14 views
1

我試圖找到最便宜的guitarRig。吉他鑽具有來自產品的零件(例如,amplifiercabinet,microphone,guitarType,guitarStringType,patchCord,effectsPedal)。 產品的購買價格由我的PurchaseInformation表格決定。MySQL:我如何將多個表聯繫在一起

這裏是我的表:

Table Part: 
    name 
    guitarRig references GuitarRig(name) 
    product references Product(name) 

Table Product: 
    name 
    part references Part(name) 
    barcodeNumber 

Table PurchaseInformation: 
    price 
    product references Product(name)  
    purchasedFrom references Store(name) 

到目前爲止,我有什麼是這樣的:

SELECT guitarRig 
FROM Part 
WHERE product = 
(
    SELECT name 
    FROM Product 
    WHERE name = 
    (
     SELECT product, MIN(price) as minPrice 
     FROM PurchaseInformation 
     GROUP BY minPrice 
    ) 
); 

我意識到這有許多與此問題(即我想說name等於兩列,productminPrice),但我不能爲我的生活找出如何得到我正在尋找的。

回答

0

我還沒有測試過,所以可能會有一些錯別字。我的假設是,最便宜的guitarRig是通過總結該鑽機的所有零件價格來確定的。

SELECT p.guitarRig 
    , sum(r.price) as totPrice 
    FROM Part p 
    , Product r 
    , PurchaseInformation i 
WHERE p.product = r.name 
    AND p.name = r.part 
    AND r.name = i.product 
GROUP BY p.guitarRig 
ORDER BY totPrice desc 
LIMIT 1 
相關問題