JOIN和SUM不同的语句结果(Wordpress-Mailster数据库)
JOIN and SUM different statement results (Wordpress-Mailster Database)
在 Mailster(wordpress 的电子邮件营销插件)的最后一次更新后,他们改变了存储有关打开、点击、取消订阅的信息的方式...
到目前为止,所有内容都存储在两个数据库中:
- bao_posts: 与任何其他 wordpress post 一样,信息
发送的电子邮件在那里。 (当post_type = 'newsletter')
- bao_mailster_actions:这是用户与
电子邮件被存储。 1 当它被发送给一个人时,2 当他们
打开它,点击它时 3 次,取消订阅时 4 次。
通过这个查询,我可以获得一个 table,其中包含所有电子邮件及其打开、点击、取消订阅的信息...
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 ;
*此查询的预期结果位于 post。
现在的问题是在更新之前,这个设置是为电子邮件保留的,但它已经为新的改变了,现在 bao_mailster_actions 分为:
- bao_mailster_action_sent
- bao_mailster_action_opens
- bao_mailster_action_clicks
- bao_mailster_action_unsubscribes
我知道如何像这样获取每个 table 的计数:
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 个新 table 中的每一个)。
所以我想做的是将这 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
Segundo email
305
97
56
0
提前致谢!
我建议您使用 UNION ALL 来连接一个 CTE.You 中的所有表,然后可以在您的查询中使用它。我已经修改了名称,因为我们不能记录同名的。
> create table if not exists bao_mailster_action_sent
( campaign_id int,count int);
create table if not exists bao_mailster_action_opens
( campaign_id int,count int);
create table if not exists bao_mailster_action_clicks
( campaign_id int,count int);
create table if not exists bao_mailster_action_unsubscribes
( campaign_id int,count int);
CREATE TABLE if not exists bao_posts(
post_modified date,
ID int,
post_title varchar(50) );
insert into bao_mailster_action_sent values
(1,88),(2,4),(4,6);
insert into bao_mailster_action_opens values
(2,4),(3,5),(4,10);
insert into bao_mailster_action_clicks values
(1,3),(2,3),(4,6);
insert into bao_mailster_action_unsubscribes values
(1,4),(3,5),(4,5);
INSERT INTO bao_posts values
( '2021-03-01',1,'first post'),
( '2021-06-01',2,'second opion'),
( '2021-09-01',3,'third way'),
( '2021-12-01',4,'last post');
WITH bao_mailster_actionsent AS
( SELECT campaign_id,count, 1 type FROM
bao_mailster_action_sent
UNION ALL
SELECT campaign_id,count,2 FROM
bao_mailster_action_opens
UNION ALL
SELECT campaign_id,count,3 FROM
bao_mailster_action_clicks
UNION ALL
SELECT campaign_id,count,4 FROM
bao_mailster_action_unsubscribes)
SELECT bao_mailster_actionsent.campaign_id,
COUNT(bao_mailster_actionsent.count) AS TotalCount,
SUM(bao_mailster_actionsent.count) AS TotalNumber,
'type'
FROM bao_mailster_actionsent
GROUP BY bao_mailster_actionsent.campaign_id,'type' ;
WITH baoMailsterAction AS
( SELECT campaign_id,count, 1 type FROM
bao_mailster_action_sent
UNION ALL
SELECT campaign_id,count,2 FROM
bao_mailster_action_opens
UNION ALL
SELECT campaign_id,count,3 FROM
bao_mailster_action_clicks
UNION ALL
SELECT campaign_id,count,4 FROM
bao_mailster_action_unsubscribes)
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
campaign_id | TotalCount | TotalNumber | type
----------: | ---------: | ----------: | ---:
1 | 1 | 88 | 1
2 | 1 | 4 | 1
4 | 1 | 6 | 1
2 | 1 | 4 | 2
3 | 1 | 5 | 2
4 | 1 | 10 | 2
1 | 1 | 3 | 3
2 | 1 | 3 | 3
4 | 1 | 6 | 3
1 | 1 | 4 | 4
3 | 1 | 5 | 4
4 | 1 | 5 | 4
post_modified | ID | post_title | Number_People_Reached | Opens | Clicks | Unsubs
:------------ | -: | :----------- | --------------------: | ----: | -----: | -----:
2021-03-01 | 1 | first post | 1 | 0 | 1 | 1
2021-06-01 | 2 | second opion | 1 | 1 | 1 | 0
2021-09-01 | 3 | third way | 0 | 1 | 0 | 1
2021-12-01 | 4 | last post | 1 | 1 | 1 | 1
db<>fiddle here
我终于只使用 Mailster 创建的新表就可以工作了(看来他们最终确实通过更新将所有信息移到了新表中) 并且4 个左连接。
我留下代码以防其他人发现它有用:
SELECT P.post_modified,
P.ID,
P.post_title,
IFNULL(S.count,0) as 'Total',
IFNULL(O.count,0) as 'Aperturas',
IFNULL(C.count,0) as 'Clicks',
IFNULL(U.count,0) as 'Bajas' from bao_posts as P
LEFT JOIN (select campaign_id, count(DISTINCT subscriber_id) as count from bao_mailster_action_clicks group by campaign_id) as C ON C.campaign_id = P.ID
LEFT JOIN (select campaign_id, count(DISTINCT subscriber_id) as count from bao_mailster_action_opens group by campaign_id) as O ON O.campaign_id = P.ID
LEFT JOIN (select campaign_id, count(DISTINCT subscriber_id) as count from bao_mailster_action_sent group by campaign_id) as S ON S.campaign_id = P.ID
LEFT JOIN (select campaign_id, count(DISTINCT subscriber_id) as count from bao_mailster_action_unsubs group by campaign_id) as U ON U.campaign_id = P.ID
WHERE P.post_type = 'newsletter'
ORDER BY P.post_modified ASC ;
P.S:如我所料,Mailster 的支持根本没有帮助:'(
在 Mailster(wordpress 的电子邮件营销插件)的最后一次更新后,他们改变了存储有关打开、点击、取消订阅的信息的方式...
到目前为止,所有内容都存储在两个数据库中:
- bao_posts: 与任何其他 wordpress post 一样,信息 发送的电子邮件在那里。 (当post_type = 'newsletter')
- bao_mailster_actions:这是用户与 电子邮件被存储。 1 当它被发送给一个人时,2 当他们 打开它,点击它时 3 次,取消订阅时 4 次。
通过这个查询,我可以获得一个 table,其中包含所有电子邮件及其打开、点击、取消订阅的信息...
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 ;
*此查询的预期结果位于 post。
现在的问题是在更新之前,这个设置是为电子邮件保留的,但它已经为新的改变了,现在 bao_mailster_actions 分为:
- bao_mailster_action_sent
- bao_mailster_action_opens
- bao_mailster_action_clicks
- bao_mailster_action_unsubscribes
我知道如何像这样获取每个 table 的计数:
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 个新 table 中的每一个)。
所以我想做的是将这 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 | Segundo email | 305 | 97 | 56 | 0 |
提前致谢!
我建议您使用 UNION ALL 来连接一个 CTE.You 中的所有表,然后可以在您的查询中使用它。我已经修改了名称,因为我们不能记录同名的。
> create table if not exists bao_mailster_action_sent
( campaign_id int,count int);
create table if not exists bao_mailster_action_opens
( campaign_id int,count int);
create table if not exists bao_mailster_action_clicks
( campaign_id int,count int);
create table if not exists bao_mailster_action_unsubscribes
( campaign_id int,count int);
CREATE TABLE if not exists bao_posts(
post_modified date,
ID int,
post_title varchar(50) );
insert into bao_mailster_action_sent values
(1,88),(2,4),(4,6);
insert into bao_mailster_action_opens values
(2,4),(3,5),(4,10);
insert into bao_mailster_action_clicks values
(1,3),(2,3),(4,6);
insert into bao_mailster_action_unsubscribes values
(1,4),(3,5),(4,5);
INSERT INTO bao_posts values
( '2021-03-01',1,'first post'),
( '2021-06-01',2,'second opion'),
( '2021-09-01',3,'third way'),
( '2021-12-01',4,'last post');
WITH bao_mailster_actionsent AS
( SELECT campaign_id,count, 1 type FROM
bao_mailster_action_sent
UNION ALL
SELECT campaign_id,count,2 FROM
bao_mailster_action_opens
UNION ALL
SELECT campaign_id,count,3 FROM
bao_mailster_action_clicks
UNION ALL
SELECT campaign_id,count,4 FROM
bao_mailster_action_unsubscribes)
SELECT bao_mailster_actionsent.campaign_id,
COUNT(bao_mailster_actionsent.count) AS TotalCount,
SUM(bao_mailster_actionsent.count) AS TotalNumber,
'type'
FROM bao_mailster_actionsent
GROUP BY bao_mailster_actionsent.campaign_id,'type' ;
WITH baoMailsterAction AS
( SELECT campaign_id,count, 1 type FROM
bao_mailster_action_sent
UNION ALL
SELECT campaign_id,count,2 FROM
bao_mailster_action_opens
UNION ALL
SELECT campaign_id,count,3 FROM
bao_mailster_action_clicks
UNION ALL
SELECT campaign_id,count,4 FROM
bao_mailster_action_unsubscribes)
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
campaign_id | TotalCount | TotalNumber | type ----------: | ---------: | ----------: | ---: 1 | 1 | 88 | 1 2 | 1 | 4 | 1 4 | 1 | 6 | 1 2 | 1 | 4 | 2 3 | 1 | 5 | 2 4 | 1 | 10 | 2 1 | 1 | 3 | 3 2 | 1 | 3 | 3 4 | 1 | 6 | 3 1 | 1 | 4 | 4 3 | 1 | 5 | 4 4 | 1 | 5 | 4 post_modified | ID | post_title | Number_People_Reached | Opens | Clicks | Unsubs :------------ | -: | :----------- | --------------------: | ----: | -----: | -----: 2021-03-01 | 1 | first post | 1 | 0 | 1 | 1 2021-06-01 | 2 | second opion | 1 | 1 | 1 | 0 2021-09-01 | 3 | third way | 0 | 1 | 0 | 1 2021-12-01 | 4 | last post | 1 | 1 | 1 | 1
db<>fiddle here
我终于只使用 Mailster 创建的新表就可以工作了(看来他们最终确实通过更新将所有信息移到了新表中) 并且4 个左连接。
我留下代码以防其他人发现它有用:
SELECT P.post_modified,
P.ID,
P.post_title,
IFNULL(S.count,0) as 'Total',
IFNULL(O.count,0) as 'Aperturas',
IFNULL(C.count,0) as 'Clicks',
IFNULL(U.count,0) as 'Bajas' from bao_posts as P
LEFT JOIN (select campaign_id, count(DISTINCT subscriber_id) as count from bao_mailster_action_clicks group by campaign_id) as C ON C.campaign_id = P.ID
LEFT JOIN (select campaign_id, count(DISTINCT subscriber_id) as count from bao_mailster_action_opens group by campaign_id) as O ON O.campaign_id = P.ID
LEFT JOIN (select campaign_id, count(DISTINCT subscriber_id) as count from bao_mailster_action_sent group by campaign_id) as S ON S.campaign_id = P.ID
LEFT JOIN (select campaign_id, count(DISTINCT subscriber_id) as count from bao_mailster_action_unsubs group by campaign_id) as U ON U.campaign_id = P.ID
WHERE P.post_type = 'newsletter'
ORDER BY P.post_modified ASC ;
P.S:如我所料,Mailster 的支持根本没有帮助:'(