【问题标题】:MySQL, get sum of two queriesMySQL,获取两个查询的总和
【发布时间】:2018-07-16 11:26:53
【问题描述】:

我有三个关于 Product 的不同表格,它们具有不同的列和结构,假设

Product1、Product2、Product3

所以,我试图获取具有相同user_id 的三个表的计数(*)总和,即 foreach user_id 字段。

表格 - 产品 1

select P.user_id, count(*) 
from Product1 P  
group by P.user_id

表格 - 产品 2

select P.user_id, count(*) 
from Product2 P  
group by P.user_id

表格 - 产品 3

select P.user_id, count(*) 
from Product3 P  
group by P.user_id

他们给我user_id 字段和count(*),

我可以添加 count(*) 的结果,foreach user_id 字段吗?谢谢,提前

【问题讨论】:

  • 列,而不是字段。考虑UNION ALL。
  • “虽然有 [the] 相同的 user_id [field]”是什么意思?请编辑您的问题以明确。使用足够的单词和句子来表达你的意思。也请阅读minimal reproducible example并采取行动。
  • 谢谢,@philipxy。我做了一些更改以使其更清晰

标签: mysql sql outer-join


【解决方案1】:

拥有三个结构相同的表通常表示数据库设计不佳。您应该想办法将这些表组合成一个表。

无论如何,您都可以汇总结果。一种方法是:

select user_id, sum(cnt)
from ((select user_id, count(*) as cnt
       from product1
       group by user_id
      ) union all
      (select user_id, count(*) as cnt
       from product2
       group by user_id
      ) union all
      (select user_id, count(*) as cnt
       from product3
       group by user_id
      )
     ) up
group by user_id;

您想使用union all 而不是join,因为MySQL 不支持full outer join。 Union all 确保包含所有三个表中的用户。

两次聚合(在子查询和外部查询中)允许 MySQL 将索引用于内部聚合。这可能是性能优势。

此外,如果您要查找特定用户或一组用户,请在子查询中使用where 子句。这比将所有数据放在子查询中然后进行过滤更有效(在 MySQL 中)。

【讨论】:

  • 你说得对,表结构。但它是因为需要项目和表结构而设计的
【解决方案2】:

使用UNION 组合结果,然后进行加法。

查询

select t.`user_id`, sum(`count`) as `total` from(
    select `user_id`, count(*) as `count`
    from `Product1`
    group by `user_id`
    union all
    select `user_id`, count(*) 
    from `Product2` 
    group by `user_id`
    union all
    select `user_id`, count(*) 
    from `Product3`
    group by `user_id`
) t
group by t.`user_id`;

【讨论】:

    【解决方案3】:

    你可以把 union all 的结果求和

    select user_id, sum(my_count)
    from (
    
    select P.user_id, count(*)  my_count
    from Product1 P  
    group by P.user_id
    UNION ALL
    select P.user_id, count(*) 
    from Product2 P  
    group by P.user_id
    UNION ALL 
    select P.user_id, count(*) 
    from Product3 P  
    group by P.user_id ) t
    group by user_id
    

    【讨论】:

      【解决方案4】:

      是的,你可以:)

      SELECT SUM(userProducts) userProducts 
      FROM (
          SELECT count(user_id) userProducts FROM Product1 WHERE user_id = your_user_id
          UNION ALL
          SELECT count(user_id) userProducts FROM Product2 WHERE user_id = your_user_id
          UNION ALL
          SELECT count(user_id) userProducts FROM Product3 WHERE user_id = your_user_id
      ) s
      

      【讨论】:

        【解决方案5】:

        请在下面尝试。未在 db 中尝试过,因此可能会出现语法错误。

        选择 p.user_id ,sum(total) from ( select P.user_id, count() total from product1 p group by P.user_id 联合所有 select P.user_id, count() total from product2 p group by P.user_id 联合所有 select P.user_id, count(*) total from product3 p group by P.user_id ) 一个

        【讨论】:

          【解决方案6】:

          是的,我们可以根据我们的要求使用join 和union 聚合来自不同tables 的结果。在您的情况下,Union All 将完美运行,并且可以使用count(1) 而不是count(*) 编写优化查询,因为它使用table 的第一个索引,通常是聚集索引。

          select user_id, sum(cnt)
          from ((select user_id, count(1) as cnt
                 from product1
                 group by user_id
                ) union all
                (select user_id, count(1) as cnt
                 from product2
                 group by user_id
                ) union all
                (select user_id, count(1) as cnt
                 from product3
                 group by user_id
                )
               ) a
          group by user_id;
          

          【讨论】:

            猜你喜欢
            • 2019-04-29
            • 2021-01-26
            • 1970-01-01
            • 2011-01-26
            • 2019-10-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-10-20
            相关资源
            最近更新 更多