在具有内部联接的更新查询中使用SUM函数

时间:2016-12-07 20:25:10

标签: mysql sql sql-update

运行此

时出错
UPDATE FlightBooking
INNER JOIN Passenger ON Passenger.FlightBookingId=FlightBooking.FlightBookingId 
INNER JOIN AirplaneSeat ON AirplaneSeat.AirplaneSeatId =  Passenger.SeatId
INNER JOIN Section ON AirplaneSeat.SectionId = Section.SectionId
INNER JOIN ExtraCost ON ExtraCost.FlightBookingId=FlightBooking.FlightBookingId
INNER JOIN Luggage ON Luggage.LuggageId = ExtraCost.LuggageId
INNER JOIN SportsEquipment ON ExtraCost.SportsEquipmentId=SportsEquipment.SportsEquipmentId
INNER JOIN Insurance ON ExtraCost.InsuranceId = Insurance.InsuranceId
INNER JOIN CarHirePrice ON CarHirePrice.CarHirePriceId= ExtraCost.CarHirePriceId
INNER JOIN Route ON FlightBooking.RouteId = Route.RouteId
SET FlightBooking.TotalCost = (SUM(Section.PriceInflux+Route.RoutePrice+Luggage.Price+SportsEquipment.SportsEquipmentPrice+Insurance.Price+CarHirePrice.TotalPrice)) 
WHERE FlightBooking.FlightBookingId=1;

当我将其形成为Select查询时,它返回正确的值,以便所有表都可以。我假设我的语法错误。

任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:0)

在update语句中使用聚合时,应使用以下语法。

UPDATE t1
 SET t1.field = t2.field2Sum
 FROM table1 t1
 INNER JOIN (select field3, sum(field2) as field2Sum
    from table2
       group by field3) as t2
 on t2.field3 = t1.field3  

见下文,我只编写了一些表,但你可以看到如何完成其​​余的工作。

UPDATE FlightBooking set TotalCost = (ExtraCost.SumExtra + SumLuggage)  From FlightBooking 
INNER JOIN Passenger ON Passenger.FlightBookingId=FlightBooking.FlightBookingId 
INNER JOIN AirplaneSeat ON AirplaneSeat.AirplaneSeatId =  Passenger.SeatId
INNER JOIN Section ON AirplaneSeat.SectionId = Section.SectionId
INNER JOIN (Select FlightBookingId, sum(ExtraCost) as SumExtra from Extracost Group by FlightBookingId) as  ExtraCost
     ON ExtraCost.FlightBookingId=FlightBooking.FlightBookingId
INNER JOIN (Select FlightBookingId, sum(Price) as SumLuggage from Luggage Group by FlightBookingId) as  Luggage
     ON Luggage.FlightBookingId=FlightBooking.FlightBookingId

答案 1 :(得分:0)

您确定您有多条匹配单个预订ID的记录吗?

如果没有,您可以免除sum()

SET FlightBooking.TotalCost = (Section.PriceInflux+Route.RoutePrice+Luggage.Price+SportsEquipment.SportsEquipmentPrice+Insurance.Price+CarHirePrice.TotalPrice)

如果没有,您需要预先聚合可能生成多行的表。事实上,无论如何你都需要这样做以获得有效的结果(连接生成的笛卡尔积将甩掉总和)。

答案 2 :(得分:0)

使用聚合函数与更新和内部联接的许多不同可能性

  UPDATE flight
  SET flight.TotalCost = t.sumPrice
  FROM FlightBooking AS flight
  INNER JOIN
      (
        SELECT    SUM(Section.PriceInflux+Route.RoutePrice+Luggage.Price+SportsEquipment.SportsEquipmentPrice+Insurance.Price+CarHirePrice.TotalPrice)) as sumPrice
        FROM Passenger ON Passenger.FlightBookingId=flight.FlightBookingId 
        INNER JOIN AirplaneSeat ON AirplaneSeat.AirplaneSeatId =  Passenger.SeatId
        INNER JOIN Section ON AirplaneSeat.SectionId = Section.SectionId
        INNER JOIN ExtraCost ON ExtraCost.FlightBookingId=flight.FlightBookingId
        INNER JOIN Luggage ON Luggage.LuggageId = ExtraCost.LuggageId
        INNER JOIN SportsEquipment ON ExtraCost.SportsEquipmentId=SportsEquipment.SportsEquipmentId
        INNER JOIN Insurance ON ExtraCost.InsuranceId = Insurance.InsuranceId
        INNER JOIN CarHirePrice ON CarHirePrice.CarHirePriceId= ExtraCost.CarHirePriceId
        INNER JOIN Route ON flight.RouteId = Route.RouteId
      ) t
     WHERE flight.bookingID = 1

也看看这个例子 PC @PauloSantos和@OMGPonies https://stackoverflow.com/a/2009981/4426282