通过多个外键联接三个表并按日期将它们分组

时间:2020-01-15 14:59:10

标签: mysql sql join

我有三个共享通用唯一ID和其他字段的表,例如commentspoststags

评论:

id | user_id | country_id | zone_id | created_at

帖子:

id | user_id | country_id | zone_id | created_at

标签:

id | user_id | country_id | zone_id | created_at

我现在想做的是获取评论,帖子和标签的行数,这些行是按created_at列按天分组,并按user_id,{ {1}}和country_id,类似于:

zone_id

问题在于所有三个表都具有数百万行,因此我想使用没有重复的连接。我想出了这个:

date | user_id | country_id | zone_id | count(comments.id) | count(posts.id) | count(tags.id)

令人惊讶的是,这给出了正确的结果,但是由于连接,它给出了很多重复的行-这很糟糕,因为在将来我可能会改用select date(c.datetime), c.user_id, c.country_id, c.zone_id, count(distinct(c.id)), count(distinct(p.id)), count(distinct(t.id)) from comments c inner join posts p inner join tags t group by date(c.datetime), c.user_id, c.country_id, c.zone_id; ,而不再使用SUM

如何通过这3个外键(DISTINCTuser_idcountry_id)将这三个表联接起来,这样我只能得到不同的行?

1 个答案:

答案 0 :(得分:1)

我认为union all将提供准确的计数:

select dte, user_id, country_id,
       sum(is_comment), sum(is_post), sum(is_tag)
from ((select date(created_at) as dte, user_id, country_id, 1 as is_comment, 0 as is_post, 0 as is_tag
       from comments
      ) union all
      (select date(created_at) as dte, user_id, country_id, 1 as is_comment, 0 as is_post, 0 as is_tag
       from posts
      ) union all
      (select date(created_at) as dte, user_id, country_id, 1 as is_comment, 0 as is_post, 0 as is_tag
       from tags
      )
     ) cpt
group by dte, user_id, country_id
相关问题