运行此查询后,我收到一条错误消息,指出无效使用组功能。我非常肯定在案件陈述之后,我需要一个“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 :(得分:0)
可能您应该在金额之前删除总和。
答案 1 :(得分: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问题很明显 - 你在sums中有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
上的其他条件来解决此问题。