有多少字段(mysql)

elcex8rz  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(263)

我有一张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...Bookingsweek_numbermonth_numberJanuary
Location One321January
Location One531January
Location One241January
Location One252February
Location One562February
Location One172February
Location One382February
有很多地方。
如何在查询中合并每周30个guest字段中的guest数和位置?
谢谢你的帮助。
xbp102n0

xbp102n01#

简单地计算每个组合的客人数,然后把这些数加起来

SELECT location,week_number,month_number,SUM(
(guest_1 <> "") +
(guest_2 <> "") +
(guest_3 <> "") +
....
(guest_30 <> "")
) AS num_guests
FROM locations
GROUP BY location,week_number,month_number
mzillmmw

mzillmmw2#

这很困难,因为这个表没有标准化。在我看来,这是可能的,请重新创建这个解决方案,并创建新的表访客与外键从位置。
下一步是创建子查询,例如:

(you query) t1
having 30 = (select count(*) from GUEST where idlocation=t1.idlocation and year=t1.year, week_number=t1.week_number)

这将是更好的解决方案,因为在这种情况下,您可以添加更多的客户机而不更改逻辑表。在你的情况下,你总是单独检查每一列。

相关问题