[英]C# SQL table select and count distinct values
i have ASP website with SQL database table.我有一个带有 SQL 数据库表的 ASP 网站。 in this table column mane "type".在这个表列鬃毛“类型”。 i want to get all distinct values from this column with values count in datatable.我想从该列中获取所有不同的值,并使用数据表中的值计数。 for example for database table:例如对于数据库表:
id type
---------
1 type1
2 type2
3 type3
4 type2
5 type2
6 type3
i want to get the following datatable:我想获得以下数据表:
type count
------------
type1 1
type2 3
type3 2
You can use您可以使用
SELECT type, COUNT(type) as count
FROM Table1
GROUP BY type
Result结果
| type | count |
|-------|-------|
| type1 | 1 |
| type2 | 3 |
| type3 | 2 |
I would suggest that you do this in sql itself by using following query我建议您使用以下查询在 sql 本身中执行此操作
SELECT type, COUNT(type) as count
FROM myTableName
GROUP BY type
SQL SQL
SELECT type, COUNT(type) as count
FROM Table1
GROUP BY type
Linq林克
from t in <YourEntityObject>.YourTableName
group t by t.type into myVar
select new
{
k = myVar.Key,
c = myVar.Count()
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.