数据库表计数相同的值 [英] Database table count same value

查看:49
本文介绍了数据库表计数相同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库表,其中用户ID具有另一个用户的外国ID,例如:



id | refer_id

1 ....... 98

2 ....... 7

3 .. ..... 4

4 ....... 98

5 ....... 87

6。 ...... 13

7 ....... 4

8 ....... 4



我想查看整个表并计算出有2(98),1(7),3(4),1(13)等等。

我是数据库的新手并且混淆了查询的工作方式。

I have a database table which users id had a foreign id of another user like:

id | refer_id
1.......98
2.......7
3.......4
4.......98
5.......87
6.......13
7.......4
8.......4

I want to check the whole table and count that there''s 2(98),1(7),3(4),1(13) and so on.
I am new to database and confuse how the query going to work.

推荐答案

试试这个:

Try this:
SELECT refer_id, COUNT(refer_id) AS CountOfReferId
FROM YourTable
GROUP BY refer_id
ORDER BY refer_id





更多关于:聚合函数 - MySQL 5.0参考 [ ^ ]

MySQL集合函数 [ ^ ]



获取refer_id'的位置 COUNT( refer_id)> = 4



More about: Aggregate functions - MySQL 5.0 Reference[^]
MySQL aggregate functions[^]

To get refer_id''s where COUNT(refer_id)>=4

SELECT refer_id, COUNT(refer_id)
FROM Table1
GROUP BY refer_id
HAVING COUNT(refer_id)>=4





获取 SUM(上图)



To get SUM(above)

SELECT SUM(T.CountOfReferId) AS SumOfCountReferId_4
FROM (
    SELECT refer_id, COUNT(refer_id) AS CountOfReferId
    FROM Table1
    GROUP BY refer_id) AS T
WHERE T.CountOfReferId >=4


这篇关于数据库表计数相同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆