JOIN 和 SUM 不同的语句结果(Wordpress-Mailster 数据库)
在上次更新 Mailster(wordpress 电子邮件营销插件)后,他们更改了存储打开信息的方式、点击、取消订阅...
到目前为止,所有内容都存储在两个数据库中:
- bao_posts:与任何其他 WordPress 帖子一样,帖子的信息 发送的电子邮件就在那里。 (当post_type = 'newsletter'时)
- bao_mailster_actions:这是用户的操作与 电子邮件已存储。 1 当它被发送给一个人时,2 当他们 打开它,点击它时为 3,取消订阅时为 4。
通过这个查询,我可以获得一个表格,其中包含所有电子邮件及其打开、点击、取消订阅的信息...
SELECT bao_posts.post_modified,
bao_posts.ID,
bao_posts.post_title,
COUNT(CASE WHEN bao_mailster_actions.type = 1 then 1 ELSE NULL END) AS Number_People_Reached,
COUNT(CASE WHEN bao_mailster_actions.type = 2 then 1 ELSE NULL END) AS Opens,
COUNT(CASE WHEN bao_mailster_actions.type = 3 then 1 ELSE NULL END) AS Clicks,
COUNT(CASE WHEN bao_mailster_actions.type = 4 then 1 ELSE NULL END) AS Unsubs
FROM bao_posts
LEFT JOIN bao_mailster_actions ON bao_mailster_actions.campaign_id = bao_posts.ID
WHERE bao_posts.post_type = 'newsletter'
GROUP BY bao_posts.ID ;
*此查询的预期结果位于帖子末尾。
现在的问题是,此设置在更新之前保留用于电子邮件,但它已更改为新的电子邮件,现在 bao_mailster_actions 分为:
- bao_mailster_action_sent
- bao_mailster_action_opens
- bao_mailster_action_clicks
- bao_mailster_action_unsubscribes
我知道如何获取每个表的计数,如下所示
SELECT bao_mailster_action_sent.campaign_id,
COUNT(bao_mailster_action_sent.count) AS Number_People_Reached
FROM bao_mailster_action_sent
GROUP BY bao_mailster_action_sent.campaign_id;
: :
campaign_id | Number_People_Reached |
---|---|
9785 | 300 |
9786 | 305 |
(这 4 个新表依次类推)。
所以我想做的是将这 4 个新查询加入到原来的查询中。 我一直在尝试组合不同的 JOIN,但我不太明白该怎么做
*请记住,如果电子邮件 ID 两者都匹配,我需要它来添加它们的点击次数、打开次数(或其他内容)。
预期结果将是这样的(与第一个查询相同,但具有聚合数据):
post_modified | ID | post_title | Number_People_Reached | Opens | Clicks | Unsubs |
---|---|---|---|---|---|---|
2021-04-29 13:13:03 | 9785 | Prueba email | 300 | 102 | 30 | 1 |
2021-04- 30 15:12:01 | 9786 | 第二电子邮件 | 305 | 97 | 56 | 0 |
提前致谢!
After the last update of Mailster (email marketing plugin for wordpress), they have changed the way they store the information about opens, clicks, unsubscribes...
Until now, everything was stored in two databases:
- bao_posts: Like any other wordpress post, the information of the
email that is sent was there. (When the post_type = 'newsletter') - bao_mailster_actions: This is where the user's actions with the
email were stored. 1 when it was sent to a person, 2 when they
opened it, 3 when they clicked on it and 4 when they unsubscribed.
And with this query, I could get a table with all the emails and the information of their openings, clicks, unsubscribed...
SELECT bao_posts.post_modified,
bao_posts.ID,
bao_posts.post_title,
COUNT(CASE WHEN bao_mailster_actions.type = 1 then 1 ELSE NULL END) AS Number_People_Reached,
COUNT(CASE WHEN bao_mailster_actions.type = 2 then 1 ELSE NULL END) AS Opens,
COUNT(CASE WHEN bao_mailster_actions.type = 3 then 1 ELSE NULL END) AS Clicks,
COUNT(CASE WHEN bao_mailster_actions.type = 4 then 1 ELSE NULL END) AS Unsubs
FROM bao_posts
LEFT JOIN bao_mailster_actions ON bao_mailster_actions.campaign_id = bao_posts.ID
WHERE bao_posts.post_type = 'newsletter'
GROUP BY bao_posts.ID ;
*Expected result of this query at the end of the post.
Now the problem is that this setting is kept for emails before the update, but it has changed for new ones and now bao_mailster_actions is separated into:
- bao_mailster_action_sent
- bao_mailster_action_opens
- bao_mailster_action_clicks
- bao_mailster_action_unsubscribes
I know how to get the count of each of these tables like this:
SELECT bao_mailster_action_sent.campaign_id,
COUNT(bao_mailster_action_sent.count) AS Number_People_Reached
FROM bao_mailster_action_sent
GROUP BY bao_mailster_action_sent.campaign_id;
To get:
campaign_id | Number_People_Reached |
---|---|
9785 | 300 |
9786 | 305 |
(And so on with each of these 4 new tables).
So what I would like to do would be to join these 4 new queries to the original one. I've been trying to combine different JOINs, but I don't quite understand how to do it.
*Bearing in mind that if an email ID matches in both, I would need it to add up their clicks, opens (or whatever).
The expected outcome would be something like this (the same as the first query but with the aggregate data):
post_modified | ID | post_title | Number_People_Reached | Opens | Clicks | Unsubs |
---|---|---|---|---|---|---|
2021-04-29 13:13:03 | 9785 | Prueba email | 300 | 102 | 30 | 1 |
2021-04-30 15:12:01 | 9786 | Segundo email | 305 | 97 | 56 | 0 |
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议您使用 UNION ALL 来连接 CTE 中的所有表。然后您可以在查询中使用它。我修改了名称,因为我们不能有相同名称的记录。
db<>fiddle 此处
I suggest that you use UNION ALL to join all the tables in a CTE.You can then use this in your query. I have modified the name because we cannot have to records with the same name.
db<>fiddle here
我终于只使用 Mailster 创建的新表(看起来他们最终确实通过更新将所有信息移动到新表中)和 4 个 LEFT JOINS 使其正常工作。
我留下代码以防其他人发现它有用:
PS:正如我所料,Mailster 的支持根本没有帮助:'(
I finally got it to work using only the new tables that Mailster created (it seems that finally they did move all the info to the new tables with the update) and with 4 LEFT JOINS.
I leave the code in case someone else finds it useful:
P.S: As I expected, Mailster's support has not helped at all :'(