1. 查询平均值语法
select avg(要计算的值) as 别名 from 表名 select 别名=avg(要计算的值) from 表名
2.获取数据总条数
select count(*) as 别名 from 表名 select 别名=count(*) from 表名
以下是举例:
public Dictionary<string, string> keyValuePairs() { // as 后面是取的别名 //count(*) 查询所有结果 //avg 计算平均数 //CSharp 表中的字段 //SQLServerDB表中的字段 string sql = "select count(*) as total,avg(CSharp) as avgCSharp,avg(SQLServerDB) as avgSQLServerDB from ScoreList;"; //select count(*) as qCount from Students where StudentId not in (select StudentId from ScoreList)的意思是 //根据StudentId排除已经在ScoreList表中存在的StudentId,返回没有存在的 sql += "select count(*) as qCount from Students where StudentId not in (select StudentId from ScoreList)"; //这句是封装的从数据库查询 SqlDataReader sqlData = SqlHelper.GetReader(sql); //定义一个键值对集合 Dictionary<string, string> keyValues = new Dictionary<string, string>(); while (sqlData.Read()) { keyValues.Add("total", sqlData["total"].ToString()); keyValues.Add("avgCSharp", sqlData["avgCSharp"].ToString()); keyValues.Add("avgSQLServerDB", sqlData["avgSQLServerDB"].ToString()); } //跳转到另外一个结果集,因为上面是两个查询语句 if (sqlData.NextResult()) { while (sqlData.Read()) { keyValues.Add("qCount", sqlData["qCount"].ToString()); } } return keyValues; }
标签:语句,count,StudentId,sqlData,keyValues,查询,sql,select From: https://www.cnblogs.com/tlfe/p/18246455