MySQL内部连接where子句排序和限制,子查询?
以下查询中的所有内容将为每个invBlueprintTypes行生成一行,其中包含正确的信息。但我想给它添加点东西。请参见下面的代码块MySQL内部连接where子句排序和限制,子查询?,mysql,join,limit,Mysql,Join,Limit,以下查询中的所有内容将为每个invBlueprintTypes行生成一行,其中包含正确的信息。但我想给它添加点东西。请参见下面的代码块 Select blueprintType.typeID, blueprintType.typeName Blueprint, productType.typeID, productType.typeName Item, productType.portionSize, blueprintType.basePrice * 0.9 As bp
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查询意图select或任何可以执行此操作的工具
SELECT * FROM invBlueprintTypesPrices
WHERE blueprintTypeID = blueprintType.typeID
ORDER BY timestamp DESC LIMIT 1
我需要表invBlueprintTypes中的主行仍然显示,即使invBlueprintTypesPrices没有结果。限制1是因为我希望尽可能使用最新的行,但由于需要历史记录,因此不能删除较旧的数据
如果我理解正确的话,我想我需要一个子查询select,但是怎么做呢?我已经厌倦了在查询结束后添加上面带有AS blueprintPrices的确切查询,但是没有在
WHERE blueprintTypeID = blueprintType.typeID
部分是错误的焦点。我不知道为什么。有人能解决这个问题吗?你不能说你把子查询放在哪里。如果在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的最新记录。然后它加入匹配的一个。您需要使用左连接来检查invBlueprintTypesPrices中的空值。要模拟每个类型id的限制1,您可以使用MAX,或者为了真正确保只返回一条记录,请使用行号-这取决于您是否可以为每个类型id设置多个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
假设您可能有相同的最大时间戳和两个不同的记录,将上面的两个左连接替换为类似的内容,获取行号:
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
我没有提到我把子查询放在哪里,因为我尝试了几个地方,但都不起作用,我知道它应该放在哪里。但我想我现在知道了。无法让您的回复完全按照我的要求工作,但很接近。通过斯格迪斯的回答解决了这个问题。谢谢你