2013-04-01 13 views
0

運行此查詢後,我收到一條錯誤消息,提示無效使用組函數。我很確定它的案例陳述後,我需要一個「AS量」或「AS i.amount」,但mySQl不喜歡任何這些。有什麼建議麼?使用大小寫和總結語句更新

update INVITATION AS i 
LEFT JOIN REGISTRATION AS r on i.id=r.INVITATION_id 
LEFT JOIN FUNDING AS f on r.id=f.REGISTRATION_id 
SET i.funding_travel_amount = SUM(case 
when f.category = "Other" then sum(amount) 
when f.category = "Airfare" then sum(amount) 
when f.category = "Mileage" then sum(amount) 
else 0 end), i.funding_travel = "Custom" 
WHERE i.funding_hotel = "Prepaid" 
and i.funding_travel = "None" 
and i.funding_per_diem = "None" 
and (f.category = "Other" OR f.category ="Airfare" OR f.category= "Mileage") 
and f.id is not null; 
+0

我不明白你爲什麼要使用'CASE ... WHEN',如果你選擇只有這三個值的行。 – hjpotter92

回答

0

這是您的查詢格式化,因此更具有可讀性(至少對我來說):

update INVITATION i LEFT JOIN 
     REGISTRATION r 
     on i.id=r.INVITATION_id LEFT JOIN 
     FUNDING AS f on r.id=f.REGISTRATION_id 
    SET i.funding_travel_amount = 
      SUM(case when f.category = "Other" then sum(amount) 
        when f.category = "Airfare" then sum(amount) 
        when f.category = "Mileage" then sum(amount) 
        else 0 
       end), 
     i.funding_travel = "Custom" 
    WHERE i.funding_hotel = "Prepaid" and 
      i.funding_travel = "None" and 
      i.funding_per_diem = "None" and 
      (f.category = "Other" OR f.category ="Airfare" OR f.category= "Mileage") and 
      f.id is not null; 

Onee問題是顯而易見的 - 你有資金內和報表。我將與相關子查詢寫:

update INVITATION 
    SET funding_travel_amount = 
      (select SUM(case when f.category = "Other" then amount 
          when f.category = "Airfare" then amount 
          when f.category = "Mileage" then amount 
          else 0 
         end) 
      from Registration r join 
        Funding f 
        on r.id=f.REGISTRATION_id and 
        invitation.id = r.invitation_id 
      ), 
     funding_travel = "Custom" 
    WHERE funding_hotel = "Prepaid" and 
      funding_travel = "None" and 
      funding_per_diem = "None" 

這是不完全一樣的,因爲它甚至會更新量時沒有類別是avaialble。你可以在where上附加一個條件來解決這個問題。

+0

@ Gordon,當我嘗試你的查詢時,我得到了讀取組函數無效的錯誤。 – user2232926

+0

@ user2232926。 。 。我原本回答得太快。我在'sum()'中看到了'sum()',並且觸發了我的響應。我會使用相關的子查詢編寫查詢,現在我已經將其作爲答案。 –

0

也許,你應該量之前刪除總和

+0

我也試過以下內容:正如你可以告訴我這個很新。 – user2232926

+0

升級邀請AS i的 LEFT JOIN註冊爲R ON i.id = r.INVITATION_id LEFT JOIN資金爲F上r.id = f.REGISTRATION_id SET i.funding_travel_amount = (SELECT SUM(SUM(f.category = 「Other」+ f.category =「Airfare」+ f.category =「Mileage」))),i.funding_travel =「Custom」 \t WHERE i.funding_hotel =「Prepaid」 \t and i.funding_travel =「None」 \t和i.funding_per_diem = 「無」 \t和(f.category = 「其他」 OR f.category = 「機票」 OR f.category = 「里程」) \t和f.id不爲空; – user2232926

+0

好吧,我建議你分享你的表架構定義和一些示例數據,所需的結果。然後人們可以快速幫助你。 – ljh