【问题标题】:MYSQL: Count the number of times a specific integer appears in a column then making a count of how many of these are present?MYSQL:计算特定整数在列中出现的次数,然后计算其中有多少?
【发布时间】:2012-12-29 14:24:44
【问题描述】:

我有一张这样的表格

Report used                  UserID

 1                             2

 1                             2

 1                             2

 2                             2

在这种情况下,我希望计算“报告使用”列中的 1,这会给我的值 3。我可能会在此列中为不同的用户找到其中的一些,所以我想数一数我找到 3 个 1 的次数。

我已经尝试使用 SELECT COUNT 来计算特定数字,但如果你关注我,我不确定如何计算这个数字。

【问题讨论】:

  • 你的问题不是很清楚。你能准确地展示预期的结果应该是什么样子吗?
  • 所以这里的结果将是 3,因为数字“1”在报告使用列中出现了 3 次。想象一下,在不同用户的报告使用列中有 3 个“1”的情况。我想统计一下这些实例。
  • 我还是不太清楚...对于您发布的具体示例数据,预期结果是1,对吧?

标签: mysql select count group-by


【解决方案1】:

试试这个:

SELECT userid, COUNT(reportused) onescount 
FROM tablename 
WHERE reportused = 1
GROUP BY userid

还要检查这个:

SELECT COUNT(userid) 
FROM (SELECT userid, COUNT(reportused) onescount 
      FROM tablename 
      WHERE reportused = 1
      GROUP BY userid) a 
WHERE onescount = 3

【讨论】:

    【解决方案2】:

    如果我猜对了:

    select Report_used,RU_count,count(*) 
    from
    (select Report_used, UserID, count(*) RU_Count 
        from t 
        group by Report_used, UserID) t1
    group by Report_used,RU_count;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-19
      • 1970-01-01
      • 1970-01-01
      • 2011-08-04
      • 1970-01-01
      • 1970-01-01
      • 2019-09-29
      相关资源
      最近更新 更多