以下查詢中的所有內容都會爲每個包含正確信息的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
部分是錯誤的焦點沒有工作。我不知道爲什麼。誰可以解決這個問題?
這工作就像一個魅力。事實上,你寫出了第二部分,因爲不同typeID的重複時間戳...很多愛... 非常感謝! – user2121823 2013-03-01 02:15:03
@ user2121823 - np,很高興能幫到你! – sgeddes 2013-03-01 02:15:56