C#:Select 语句未出现正确的结果

标签 c# mysql select

我创建了函数分析学生成绩表。目的是:找到按内容类型分组的最高平均结果。例如:

S_Id      ContentType      Result
1              T             50
1              V             70
1              G             30
1              G             40
1              V             60

我需要的输出是 V,因为 V 的平均值最高。

这是我的代码,但它没有显示正确的结果: 我的输出显示表中的第五行。

public string analyzeResultTable(string studentId)
{
    string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["AHSConnection"].ToString();
    DataSet ds = new DataSet();
    DataSet dsAns = new DataSet();
    string BestPrefrence = "";
    using (MySqlConnection conn = new MySqlConnection(connStr))
    {
        conn.Open();
            MySqlCommand cmd2 =
            new MySqlCommand(
                "Select ContentType, MAX(avgContent) from (select ContentType, AVG(result) as avgContent from dopractice where S_Id='" +
                    studentId + "' GROUP BY ContentType) AS T", conn);
            MySqlDataAdapter da = new MySqlDataAdapter(cmd2);
            da.Fill(ds);
            conn.Close();
    }

    if (ds.Tables[0].Rows.Count > 0)
    {
        BestPrefrence = ds.Tables[0].Rows[0]["ContentType"].ToString();
    }
    return BestPrefrence;
}

最佳答案

这将为您提供每个 ContentType 的平均结果

select ContentType, AVG(result) as avgContent from dopractice where S_Id='" + studentId + "' GROUP BY ContentType order by AVG(result) desc limit 1;

添加订单后您将得到您想要的

关于C#:Select 语句未出现正确的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38149026/

相关文章:

sql - 从3个表中获取值而无冗余-SQLite

c# - 将一个项目多次添加到同一个列表

mysql - SQL 连接查询尝试

java - 选择查询的 H2 数据库问题 - 结果错误

mysql - 4 列的 DISTINCT 值

javascript - Jquery - 检测点击选项禁用 HTML 选择

mysql - 如何统计where子句的结果以计算MYSQL select子句的比例?

c# - TPL 数据流 BufferBlock<> 消费者在 WPF 的主线程中工作

c# - 格式化堆栈跟踪以在 API 响应中手动返回

c# - 在字符串中找到特定的单词并打印剩余的行