[英]How to get count of records for some specific where condition in SQL
I have a data in which there are 2 columns: USER_ID and CONTRACT_ID. 我有一个数据,其中有2列:USER_ID和CONTRACT_ID。 There can be 'n' number of users in a contract id.
合同ID中可以有“ n”个用户。
1 0303000
2 0303000
3 1234567
1 0303000
2 0303000
3 1234567
1 0303000
2 0303000
3 1234567
1 0303000
2 0303000
3 1234567
Now i want something like this: 现在我想要这样的东西:
Count CONTRACT_ID
8 0303000
4 1234567
Someone please help me how to create a sql query to get this kind of result. 有人请帮助我如何创建一个sql查询以获得这种结果。
You can just do a GROUP BY
on CONTRACT_ID
and COUNT(*)
. 您可以对
CONTRACT_ID
和COUNT(*)
进行GROUP BY
。 Like this: 像这样:
SELECT
COUNT(*) as count,
CONTRACT_ID
FROM
table1
GROUP BY
CONTRACT_ID
You need to use Group By
您需要使用
Group By
依据
SELECT count(*) as `Count`,CONTRACT_ID
from tablename Group By CONTRACT_ID
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.