除了一次通过暴力破解一个查询之外,我很难找到一种可重复的高效方法来执行此操作:
- 假设我在全国有 4 个售票事件(EventA-2018, 事件 B-2018、事件 C-2018 和事件 D-2018)
- 我现在需要展示一个简单的 4x4 表格,其中包含参加 X 也参加 Y 的人数
- 每个事件都有一个关联的 MySQL 表(例如,event-a-2018-buyers、event-b-2018-buyers 等),每个事件都包含 一个名为 email 的列代表买家。
结果表应该是这样的:
+------------+-------------+-------------+-------------+-------------+ | | EventA-2018 | EventB-2018 | EventC-2018 | EventD-2018 | +------------+-------------+-------------+-------------+-------------+ |EventA-2018 | X | a | b | c | +------------+-------------+-------------+-------------+-------------+ |EventB-2018 | a | X | d | e | +------------+-------------+-------------+-------------+-------------+ |EventC-2018 | b | d | X | f | +------------+-------------+-------------+-------------+-------------+ |EventD-2018 | c | e | f | X | +------------+-------------+-------------+-------------+-------------+
So the top row basically says, "Of the people who bought tickets for EventA-2018, there were a who also bought for EventB-2018, b who also bought for EventC-2018, and c also who bought for EventD-2018".
The diagonal isn't important since it would represent 100% each time.
Out of the 12 remaining cells, I obviously only need to fill in 6 since they are repeated (e.g., a,b,c,d,e,f).
There are actually more than 4 events and each one happens each year, but I'm assuming I can adapt any solutions to expand accordingly.
My current MySQL skills stop just after doing a join on two of the event tables. So I could easily figure out the 6 inner joins I need to run to fill in this matrix and manually build the table, but I'm hoping there is a more programmatic way that will allow me to automate it into a dashboard.
Here is how I would fill in one cell at a time:
SELECT
Count( eventa_2018.email ) as cell_a
FROM
( SELECT DISTINCT email FROM eventa_2018
INNER JOIN ( SELECT DISTINCT email FROM eventb_2018 ON eventa_2018.email = eventb_2018.email;
旁注:我正在考虑的一种完全不同的方法是将所有表合并为一个只有两个字段的表 - 电子邮件、事件。然后我可以剔除所有只参加过一次事件的人。对于其余部分,我可以创建一个更简单的报告,显示参加多个事件的每个组合的人数(而上表一次只显示两个事件)。所有这一切的最终业务案例是了解在哪些方面投资更多的事件交叉推广,并创建最有值(value)的客户群。
最佳答案
不是答案。评论太长了。
规范化的模式可能看起来像这样:
event year buyer
a 2018 joe@amgil.com
b 2018 kat@plape.com
从这里开始。查看我上面的评论,然后回复我们。
关于mysql - 如何构建一个包含表之间匹配计数的矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50237530/