gpt4 book ai didi

mysql - SQL = SUM 总计

转载 作者:行者123 更新时间:2023-11-30 01:26:47 30 4
gpt4 key购买 nike

Date           Flight ID    Member ID   Seat Type   Seat Price  
2013-07-28 F71498 M69202 Business RM 40.00

F73591 M69202 First RM 50.00
M69202 First RM 50.00

F71498 M37520 Business RM 40.00
M69202 Business RM 40.00
===============
Total : ??
===============

我尝试过以下 SQL

SELECT 
r.ReservationDate, r.FlightID, r.MemberID, s.SeatType, s.SeatPrice
FROM
flight f, reservation r, seat s, member m
WHERE
r.FlightID = f.FlightID
AND r.SeatID = s.SeatID
AND r.MemberID = m.MemberID
AND ReservationDate = '2013-07-28'
AND s.FlightID = f.FlightID

我可以生成上面的报告,但我需要帮助来计算总 SeatPrice 的总和。我可以知道如何计算总计吗?谢谢先进...=)

最佳答案

如果我理解正确,您希望结果集如下所示,即最后一行具有“总计”列和总计值

Date           Flight ID    Member ID   Seat Type   Seat Price  
2013-07-28 F71498 M69202 Business RM 40.00

2013-07-28 F73591 M69202 First RM 50.00
2013-07-28 F73591 M69202 First RM 50.00

2013-07-28 F71498 M37520 Business RM 40.00
2013-07-28 F71498 M69202 Business RM 40.00

Total RM 220.0

你可以做到

SELECT 
r.ReservationDate, r.FlightID, r.MemberID, s.SeatType, s.SeatPrice
FROM
flight f, reservation r, seat s, member m
INNER JOIN reservation r
ON r.FlightID = f.FlightID
INNER JOIN seat s
ON r.SeatID = s.SeatID
INNER JOIN member m
ON r.MemberID = m.MemberID
WHERE ReservationDate = '2013-07-28'
AND s.FlightID = f.FlightID

UNION

SELECT '','','','Total',SUM(s.SeatPrice)
FROM
FROM
flight f, reservation r, seat s, member m
INNER JOIN reservation r
ON r.FlightID = f.FlightID
INNER JOIN seat s
ON r.SeatID = s.SeatID
INNER JOIN member m
ON r.MemberID = m.MemberID
WHERE ReservationDate = '2013-07-28'
AND s.FlightID = f.FlightID

关于mysql - SQL = SUM 总计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17916427/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com