4

これが私の選択です:

SELECT 
  SUM(t.amount + c.designFeeValue) as cashReceived, 
  ROUND(SUM(i.value) * (m.percentOurs / 100)) as adValue,
  m.managementFee as managementFee,
  m.productionCost as productionCost,
  5 as emailAddress,
  (
    (
      SELECT value
      FROM commission_transactions
      WHERE isDebit IS
      TRUE
    ) -
    (
      SELECT value
      FROM commission_transactions
      WHERE isDebit IS
      FALSE
    )
  ) as miscExpenses,
  (managementFee + productionCost + emailAddress + miscExpenses) as totalExpenses

これは、いくつかのエイリアスを追加する次の行のために爆撃です。

(managementFee + productionCost + emailAddress + miscExpenses) as totalExpenses

エイリアスは不明なフィールドです。

この演算のエイリアスを保持する方法はありますか? それとも、totalExpenses の計算のために各エイリアスを生成するすべての演算をやり直す必要がありますか? それはそれを行う非常に醜い方法のようです。


アップデート:

あなたの提案に従って、私は現在派生テーブルを使用しています。

SELECT 
  cashReceived,
  adValue,
  managementFee,
  productionCost,
  emailAddress,
  miscExpenses,
  adValue + managementFee + productionCost + emailAddress + miscExpenses as totalExpenses
FROM (
  SELECT
    SUM(t.amount + c.designFeeValue) as cashReceived,
    ROUND(SUM(i.value) * (m.percentOurs / 100)) as adValue,
    m.managementFee as managementFee,
    m.productionCost as productionCost,
    5 as emailAddress,
    (
      (
        SELECT value
        FROM commission_transactions
        WHERE isDebit IS TRUE
      ) -
      (
        SELECT value
        FROM commission_transactions
        WHERE isDebit IS FALSE
      )
    ) as miscExpenses  
  FROM magazines m
  JOIN insertions i ON i.magazineId = m.id
  JOIN transactions t ON t.insertionId = i.id
  JOIN contracts c ON i.contractId = c.id
  JOIN commission_transactions ct ON m.id = ct.magazineId
  WHERE m.id = 17
    AND t.isChargedBack IS FALSE
    AND t.`timestamp` >= '2013-08-01 00:00:00'
    AND t.`timestamp` < '2013-09-01 00:00:00'
    AND ct.createdDate >= '2013-08-01 00:00:00'
    AND ct.createdDate < '2013-09-01 00:00:00'
) sub;
4

1 に答える 1

4

フィールド リスト内でエイリアスを再利用することはできません。例:

mysql> select 5 as five, five + 1 as six;
ERROR 1054 (42S22): Unknown column 'five' in 'field list'

選択を別のものでラップし、そのラッパーでエイリアス計算を行う必要があります。

select *, managementFee + productionCost + emailAddress + miscExpenses) as totalExpenses
FROM (
   ... your above query here ...
)
于 2013-08-23T15:15:00.627 に答える