用一个查询计算多于一次和两次的行

时间:2019-03-05 02:37:17

标签: mysql

我有一个类似于以下内容的表:(实际上还有很多行。)

   user_id   |     text  
---------------------------    
      1      |      a
      1      |      b
      2      |      c
      3      |      d
      3      |      e
      4      |      f
      4      |      g
      4      |      h
      4      |      i
      5      |      j
      5      |      k
      5      |      l
      6      |      m

我想知道有多少个用户出现了一次以上,有多少个用户出现了两次以上(上表为(4,2))

现在我正在使用此查询,但是将数字更改为2后,我必须运行两次。

SELECT COUNT(*)
FROM (
    SELECT NULL
    FROM my_table
    GROUP BY user_id
    HAVING COUNT(*) > 1
) T1

因为表太大,所以要花很多时间。我可以在一个查询中处理这两种情况吗?

3 个答案:

答案 0 :(得分:2)

您可以通过对计数求和来在一个查询中做到这一点:

SELECT SUM(count > 1) AS more_than_once,
       SUM(count > 2) AS more_than_twice
FROM (SELECT COUNT(*) AS count
      FROM my_table
      GROUP BY user_id
      HAVING count > 1) t

输出:

more_than_once  more_than_twice
4               2

Demo on dbfiddle

答案 1 :(得分:0)

尝试像这样的伴侣

SELECT COUNT(*)
FROM (
    SELECT NULL
    FROM my_table
    GROUP BY user_id
    HAVING COUNT(*) > 1
) T1 WHERE COUNT(*) > 2

答案 2 :(得分:0)

SELECT SUM(v.gt1)  AS cnt_gt_1
     , SUM(v.eq3)  AS cnt_eq_2
     , SUM(v.gt2)  AS cnt_gt_2
  FROM ( SELECT COUNT(*) > 1 AS gt1
              , COUNT(*) = 2 AS eq2
              , COUNT(*) > 2 AS gt2 
           FROM mytable t
          GROUP 
             BY t.user_id 
         HAVING COUNT(*) > 1
       ) v 

为了提高内联视图查询的性能,我们希望索引以user_id作为前导列...例如

   ... ON my_table (user_id)