0

以下查询中的所有内容都会为每个具有正确信息的 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

所以我需要在这里输入下表及其下方的列,以便我可以使用值时间戳并按利润小时对整个结果进行排序

tablename: invBlueprintTypesPrices
columns:   blueprintTypeID, timestamp, profitHour

我需要此信息并考虑以下选择。使用选择来显示我对 JOIN/in-query 选择或任何可以做到这一点的意图。

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

即使 invBlueprintTypesPrices 没有结果,我也需要表 invBlueprintTypes 中的主行仍然显示。LIMIT 1 是因为我想要最新的行,但删除旧数据不是一个选项,因为需要历史记录。

如果我理解正确,我认为我需要一个子查询选择,但该怎么做呢?我已经厌倦了在查询关闭后使用 AS blueprintPrices 添加上面的确切查询),但没有出现错误

WHERE blueprintTypeID = blueprintType.typeID

部分是错误的焦点。我不知道为什么。谁能解决这个问题?

4

2 回答 2

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 的最新记录。然后它加入匹配的那个。

于 2013-03-01T01:50:16.433 回答
0

您需要使用 aLEFT JOIN检查 invBlueprintTypesPrices 中的 NULL 值。要模仿LIMIT 1每个 TypeId,您可以使用MAX()or 来真正确保只返回一条记录,使用行号——这取决于您是否可以为每个类型 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
于 2013-03-01T01:51:41.293 回答