0

运行此查询后,我收到一条错误消息,指出无效使用组功能。我很确定它在 case 语句之后,我需要一个“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;
4

2 回答 2

0

可能,您应该在金额之前删除总和

于 2013-04-01T17:12:04.907 回答
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 的问题很明显——在 sum 中有 sum 语句。我会用一个相关的子查询来写这个:

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" 

这并不完全相同,因为即使没有任何类别可用,它也会更新金额。您可以在where.

于 2013-04-01T18:01:22.747 回答