【发布时间】:2016-08-17 17:09:27
【问题描述】:
我有三张桌子:Events、Spots 和 Bookings。 活动有很多地方(日期时间)和 Spots 有很多预订。
我在点表中有“空格”列,在预订表中有“spot_id”。在点表中,我需要对所有空间 GROUP BY event_id 求和,在预订表中我需要计算所有预订 GROUP BY event_id。两个表都有 event_id。
我需要查询类似:
select S.event_id, sum(S.spaces), count(B.spot_id)
from spots S, bookings B
where S.event_id = B.event_id;
我需要这样的输出:
Event_id sum(Spaces) count(Bookings)
1 300 60
2 450 120
但是因为它是一对多的关系,所以我弄错了空格的总和。如果我有 60 个预订 = 60 行,因此 60 * actual-sum_of_spaces。那是因为我有多个单项赛事的位置。
如何从 event_id 和 sametime 的表位置获取空间总和获取给定 event_id 的所有预订计数。点表中的行数与预订中的行数不匹配。
【问题讨论】: