0

次のクエリのすべてが、正しい情報を含むinvBlueprintTypes行ごとに1行になります。しかし、私はそれに何かを追加しようとしています。以下のコードブロックを参照してください。

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

次の選択を念頭に置いてこの情報が必要です。selectを使用して、JOIN /in-queryselectまたはこれを実行できるものの意図を示します。

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

また、invBlueprintTypesPricesの結果がない場合でも、テーブルinvBlueprintTypesのメイン行を表示する必要があります。LIMIT 1は、可能な限り最新の行が必要なためですが、履歴が必要なため、古いデータを削除することはできません。

正しく理解していれば、サブクエリselectが必要だと思いますが、それを行うにはどうすればよいですか?クエリの終了後にASblueprintPricesを使用して上記の正確なクエリを追加するのに疲れましたが、

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これにより、サブクエリ内のすべてのの最新のレコードが識別されます。次に、一致するものに参加します。

于 2013-03-01T01:50:16.433 に答える
0

LEFT JOINinvBlueprintTypesPricesでNULL値をチェックするには、を使用する必要があります。LIMIT 1TypeIdごとを模倣するには、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
于 2013-03-01T01:51:41.293 に答える