【问题标题】:How to do a count on a union query如何对联合查询进行计数
【发布时间】:2012-07-28 17:17:28
【问题描述】:

我有以下疑问:

select distinct profile_id from userprofile_...

union

select distinct profile_id from productions_...

如何计算结果总数?

【问题讨论】:

    标签: mysql sql union aggregate-functions


    【解决方案1】:

    如果您想要所有记录的总数,那么您可以这样做:

    SELECT COUNT(*)
    FROM
    (
        select distinct profile_id 
        from userprofile_...
    
        union all
    
        select distinct profile_id 
        from productions_...
    ) x
    

    【讨论】:

    • 考虑到它来自不同的表(同名列,但来自不同的表),如果两个表中的 Id 相同,他可能会丢失结果
    • 是否应该有一个不同的来删除联合的第一部分和第二部分之间的重复项? userprofile 和 productions 中可能有重复的 profile_id。
    • 现在你已经编辑了你得到的答案和我发布的一样-.-
    • 考虑到我先发了,而且我没有编辑我的答案,这至少,真的不公平!!! :(
    • @SrinivasReddyThatiparthy 您应该考虑编辑日期,因为这不是原始答案。无论哪种方式都可以,这是很久以前的事了:D
    【解决方案2】:

    如果两个表中都有相等的行,你应该使用Union All,因为联合使一个不同的

    select count(*) from 
    (select distinct profile_id from userprofile_...
    
    union ALL
    
    select distinct profile_id from productions_...) x
    

    在这种情况下,如果你在两个表中都有相同的Profile_Id(id 可能是一个数字,所以这是可能的),那么如果你使用Union,如果你在两个tables 中都有Id = 1,你会丢失一排(它会出现一次而不是两次)

    【讨论】:

      【解决方案3】:

      这会表现得很好:

      select count(*) from (
          select profile_id
          from userprofile_...
          union
          select profile_id
          from productions_...
      ) x
      

      union 的使用保证了不同的值 - union 删除重复项,union all 保留它们。这意味着您不需要 distinct 关键字(其他答案没有利用这一事实并最终做更多的工作)。

      编辑:

      如果您想计算每个中不同 profile_id 的总数,其中出现在两个表中的给定值被视为 不同 值,请使用:

      select sum(count) from (
          select count(distinct profile_id) as count
          from userprofile_...
          union all
          select count(distinct profile_id)
          from productions_...
      ) x
      

      此查询的性能将优于所有其他答案,因为数据库可以比联合列表更快地有效地计算表中的不同值。 sum() 只是将两个计数相加。

      【讨论】:

      • 好吧,您正在考虑 profile_id 将是唯一的。如果我在产品中获得 3 个 id = 1(可能是 FK),并且在 userProfile 中获得一个 Id = 1(可能是 PK) ,然后联合将给出 1 行,而不是 4 行(两者都不同将得到 2)。然后计数将失败。在他发布的查询中,他每次选择都会得到一个id=1,计数应该是2
      • @ElVieejo 这就是我认为他想要的。编辑答案
      【解决方案4】:

      如果 COUNT(*) 之一的结果等于 0,则这些将不起作用。

      这样会更好:

      选择总和(总计) 从 ( 选择 COUNT(distinct profile_id) AS 总计 来自用户配置文件_... 联合所有 选择 COUNT(distinct profile_id) AS 总计 从制作_... ) X

      【讨论】:

        【解决方案5】:

        正如 omg ponies 已经指出,在 UNION 中使用 distinct 是没有用的,你可以在你的情况下使用 UNION ALL .....

        SELECT COUNT(*) 
        FROM 
        ( 
        select distinct profile_id from userprofile_...
        union all
        select distinct profile_id from productions_...
        ) AS t1 
        

        【讨论】:

        • DISTINCT 在UNION 上是没有用的 - 只有UNION ALL 才需要。可能不想复制别人的答案;)
        • @OMGPonies :顺便说一句,我没有复制答案....我迟到了几分之一秒才发布答案.....
        【解决方案6】:

        最好的解决方案是添加两个查询结果的计数。如果表包含大量记录,这不会有问题。而且您不需要使用联合查询。 例如:

        SELECT (select COUNT(distinct profile_id) from userprofile_...) + 
        (select COUNT(distinct profile_id) from productions_...) AS total
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-09-12
          • 2019-09-07
          相关资源
          最近更新 更多