从 3 个不同的表中确定每个用户的总成本

Determine total cost per user from 3 Different Tables

我正在 MySql 开发剧院预订系统(我的第一个 SQL 项目)。我有三个表:

Production (contains Title, BasicTicketPrice), 
Performance (PerformanceDate, PerformanceTime, Title) 
Booking (Email of person who booked, PerformanceDate, PerformanceTime, RowNumber). 

每个人都预订了两到三场演出的门票(使用他们的电子邮件预订)。

我需要编写一个查询来显示所有预订座位的支付价格,我需要输出行号、预订人的电子邮件和计算价格。

我知道我需要连接这些表并使查询显示一个名为计算价格的临时列,但我不知道如何计算价格。

我试过这个:

SELECT DISTINCT b.RowNumber, b.Email, pr.BasicTicketPrice 
FROM booking b, production pr performance p
WHERE p.Title=b.PerfDate*b.PerfTime*b.RowNumber;

SELECT CONCAT (PerfDate, PerfTime, RowNumber) AS BookingID FROM booking;

SELECT RowNumber, Email, CONCAT(PerfDate, PerfTime, RowNumber) AS BookingID FROM booking;

SELECT RowNumber, Email, CONCAT((CONCAT(PerfDate, PerfTime, RowNumber) AS BookingID 
   FROM booking)BasicTicketPrice*BookingID);

SELECT RowNumber, Email, CONCAT(PerfDate, PerfTime, RowNumber) AS BookingID INTEGER 
FROM booking;

SELECT RowNumber FROM booking
LEFT JOIN (SELECT Title FROM performance WHERE '2017-11-01 19:00:00' Email IS NULL);

但是没有用。

有什么建议吗?我将不胜感激任何想法。

假设:

  • 预订中每个预订座位一行
  • Title 成为 Production
  • 的合适主键
  • PerformanceDate, PerformanceTimePerformance
  • 的合适主复合键

您需要按照上面假定的键将三个表连接在一起。您似乎希望按预订人员将每次演出的预订分组在一起 - 如果是这样,您需要使用聚合来显示座位号(我使用 GROUP_CONCAT 来分隔它们),以及 COUNT 购买的门票并乘以票价。

SELECT 
   b.Email, prod.Title, per.PerformanceDate, per.PerformanceTime, 
   GROUP_CONCAT(RowNumber) AS BookedSeats, 
   COUNT(RowNumber) * prod.BasicTicketPrice AS TotalCost
FROM Booking b
INNER JOIN Performance per  
     ON b.PerformanceDate = per.PerformanceDate 
        AND b.PerformanceTime = per.PerformanceTime
INNER JOIN Production prod
     ON per.Title = prod.Title
GROUP BY 
   b.Email, prod.Title, per.PerformanceDate, per.PerformanceTime, prod.BasicTicketPrice
ORDER BY prod.Title, per.PerformanceDate, per.PerformanceTime;

从技术上讲,我们应该在 GROUP BY 中包含所有非聚合列,因此 prod.BasicTicketPrice 也被列出。