我有一张table,看起来像下面的样子,我想统计一下所有的预订,每周统计一下所有的客人。有30个访客字段: locations
```
| Field | Type |
| ------------ | ---------- |
| location | text |
| day_number | int(11) |
| month_number | int(11) |
| year | text |
| week_number | tinyint(4) |
| start | datetime |
| end | datetime |
| guest_1 | text |
| guest_2 | text |
| … | |
| guest_30 | text |
我正在使用:
select locaton, count(*) AS Number of bookings per week
, week_number,
month_number, month
FROM locations
GROUP BY location, week_number;
我明白了:
location | ...Bookings | week_number | month_number | January |
---|---|---|---|---|
Location One | 3 | 2 | 1 | January |
Location One | 5 | 3 | 1 | January |
Location One | 2 | 4 | 1 | January |
Location One | 2 | 5 | 2 | February |
Location One | 5 | 6 | 2 | February |
Location One | 1 | 7 | 2 | February |
Location One | 3 | 8 | 2 | February |
有很多地方。
如何在查询中合并每周30个guest字段中的guest数和位置?
谢谢你的帮助。
2条答案
按热度按时间xbp102n01#
简单地计算每个组合的客人数,然后把这些数加起来
mzillmmw2#
这很困难,因为这个表没有标准化。在我看来,这是可能的,请重新创建这个解决方案,并创建新的表访客与外键从位置。
下一步是创建子查询,例如:
这将是更好的解决方案,因为在这种情况下,您可以添加更多的客户机而不更改逻辑表。在你的情况下,你总是单独检查每一列。