以下查询中的所有内容都会为每个具有正确信息的invBlueprintTypes行生成一行。但我正在尝试添加一些东西。请参见下面的代码块。
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
所以我需要在这里得到的是下面的表及其下面的列,因此我可以使用值timestamp并按profitHour对整个结果进行排序
tablename: invBlueprintTypesPrices
columns: blueprintTypeID, timestamp, profitHour
我需要这些信息,并考虑以下选择。使用select来显示我对JOIN / in-query select的意图或者可以执行此操作的任何内容。
SELECT * FROM invBlueprintTypesPrices
WHERE blueprintTypeID = blueprintType.typeID
ORDER BY timestamp DESC LIMIT 1
即使没有来自invBlueprintTypesPrices的结果,我还需要表invBlueprintTypes中的主行仍然显示。 LIMIT 1是因为我想要最新的行,但删除旧数据不是一个选项,因为需要历史记录。
如果我理解正确,我认为我需要一个子查询选择,但如何做到这一点?我已经厌倦了在查询结束后使用AS blueprintPrices添加上面的确切查询,但是没有使用
的错误WHERE blueprintTypeID = blueprintType.typeID
部分是错误的焦点。我不知道为什么。有谁能解决这个问题?
答案 0 :(得分: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
的最新记录。然后它加入匹配的那个。
答案 1 :(得分:0)
您需要使用LEFT JOIN
来检查invBlueprintTypesPrices中的NULL值。要模仿每个TypeId LIMIT 1
,您可以使用MAX()
或者真正确保只返回单个记录,使用行号 - 这取决于您是否可以有多个最大时间戳每种类型的id。假设没有,那么这应该是关闭的:
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