2013-03-01 89 views
0

以下查詢中的所有內容都會爲每個包含正確信息的invBlueprintTypes行生成一行。但我正在嘗試添加一些內容。請參閱下面的代碼塊。MySQL內部加入where子句排序和限制,子查詢?

Select 
    blueprintType.typeID, 
    blueprintType.typeName Blueprint, 
    productType.typeID, 
    productType.typeName Item, 
    productType.portionSize, 
    blueprintType.basePrice * 0.9 As bpoPrice, 
    productGroup.groupName ItemGroup, 
    productCategory.categoryName ItemCategory, 
    blueprints.productionTime, 
    blueprints.techLevel, 
    blueprints.researchProductivityTime, 
    blueprints.researchMaterialTime, 
    blueprints.researchCopyTime, 
    blueprints.researchTechTime, 
    blueprints.productivityModifier, 
    blueprints.materialModifier, 
    blueprints.wasteFactor, 
    blueprints.maxProductionLimit, 
    blueprints.blueprintTypeID 
From 
    invBlueprintTypes As blueprints 
    Inner Join invTypes As blueprintType On blueprints.blueprintTypeID = blueprintType.typeID 
    Inner Join invTypes As productType On blueprints.productTypeID = productType.typeID 
    Inner Join invGroups As productGroup On productType.groupID = productGroup.groupID 
    Inner Join invCategories As productCategory On productGroup.categoryID = productCategory.categoryID 
Where 
    blueprints.techLevel = 1 And 
    blueprintType.published = 1 And 
    productType.marketGroupID Is Not Null And 
    blueprintType.basePrice > 0 

所以我需要在這裏得到同列於下表是低於它,所以我可以profitHour

tablename: invBlueprintTypesPrices 
columns: blueprintTypeID, timestamp, profitHour 

使用值時間戳和排序整個結果我需要這個信息以下選擇記住。使用選擇來顯示我的意圖的JOIN /查詢內選擇或任何可以做到這一點。

SELECT * FROM invBlueprintTypesPrices 
WHERE blueprintTypeID = blueprintType.typeID 
ORDER BY timestamp DESC LIMIT 1 

我需要的主要行從表invBlueprintTypes仍然顯示即使從invBlueprintTypesPrices沒有結果。限制1是因爲我想要最新的行,但是由於需要歷史記錄,刪除舊數據不是一個選項。

如果我理解正確,我想我需要一個子查詢選擇,但如何做到這一點?我已經厭倦了補充說,與AS blueprintPrices以上是查詢收盤後的精確查詢),但有一個錯誤與

WHERE blueprintTypeID = blueprintType.typeID 

部分是錯誤的焦點沒有工作。我不知道爲什麼。誰可以解決這個問題?

回答

0

您需要使用LEFT JOIN來檢查invBlueprintTypesPrices中的NULL值。爲了模擬每個TypeId的LIMIT 1,您可以使用MAX()或真正確保您只返回一條記錄,使用行號 - 這取決於您是否可以爲每個類型標識具有多個最大時間戳。假如沒有,那麼這應該是接近:

Select 
    ... 
From 
    invBlueprintTypes As blueprints 
    Inner Join invTypes As blueprintType On blueprints.blueprintTypeID = blueprintType.typeID 
    Inner Join invTypes As productType On blueprints.productTypeID = productType.typeID 
    Inner Join invGroups As productGroup On productType.groupID = productGroup.groupID 
    Inner Join invCategories As productCategory On productGroup.categoryID = productCategory.categoryID 
    Left Join (
    SELECT MAX(TimeStamp) MaxTime, TypeId 
    FROM invBlueprintTypesPrices 
    GROUP BY TypeId 
) blueprintTypePrice On blueprints.blueprintTypeID = blueprintTypePrice.typeID 
    Left Join invBlueprintTypesPrices blueprintTypePrices On 
    blueprintTypePrice.TypeId = blueprintTypePrices.TypeId AND 
    blueprintTypePrice.MaxTime = blueprintTypePrices.TimeStamp 
Where 
    blueprints.techLevel = 1 And 
    blueprintType.published = 1 And 
    productType.marketGroupID Is Not Null And 
    blueprintType.basePrice > 0 
Order By 
    blueprintTypePrices.profitHour 

假設你可能有2次不同的記錄相同的最高時間戳,更換2出現一些類似這樣獲得的行數的東西上面加入:

Left Join (
    SELECT @rn:=IF(@prevTypeId=TypeId,@rn+1,1) rn, 
     TimeStamp, 
     TypeId, 
     profitHour, 
     @prevTypeId:=TypeId 
    FROM (SELECT * 
      FROM invBlueprintTypesPrices 
      ORDER BY TypeId, TimeStamp DESC) t 
     JOIN (SELECT @rn:=0) t2 
) blueprintTypePrices On blueprints.blueprintTypeID = blueprintTypePrices.typeID AND blueprintTypePrices.rn=1 
+0

這工作就像一個魅力。事實上,你寫出了第二部分,因爲不同typeID的重複時間戳...很多愛... 非常感謝! – user2121823 2013-03-01 02:15:03

+0

@ user2121823 - np,很高興能幫到你! – sgeddes 2013-03-01 02:15:56

0

你不說你在哪兒放子查詢。如果在select條款中,那麼您遇到了問題,因爲您要返回多個值。

由於您有相關的子查詢(不允許),因此不能直接將其放入from子句中。

相反,你可以把它放在這樣的:

from . . . 
    (select * 
     from invBLueprintTypesPrices ibptp 
     where ibtp.timestamp = (select ibptp2.timestamp 
           from invBLueprintTypesPrices ibptp2 
           where ibptp.blueprintTypeId = ibptp2.blueprintTypeId 
           order by timestamp desc 
           limit 1 
          ) 
    ) ibptp 
    on ibptp.blueprintTypeId = blueprintType.TypeID 

此標識的所有blueprintTypeid S IN子查詢最近的記錄。然後加入匹配的那個。

+0

我沒有提到我放置子查詢的位置,因爲我嘗試了幾個地方,都沒有工作,我知道它應該在哪裏。但我想我現在就做。無法完全按照我的意願回覆您的回覆,但結果非常接近。通過sgeddes的答案解決它。 總之感謝 – user2121823 2013-03-01 02:14:13