Mysql 数据库列的计数图表

Graphing of count of Mysql database column

我搜索了在 c# 中绘制 sql 的计数函数。我正在尝试对每个行标签的条目进行计数并在条形图上显示计数的 table 图表。例如:

-Foreman    -count(CommentOne)
   Me                3
   you               2

我正在使用这个:

   chart1.Series.Clear();
   chart1.Series.Add("Series1");
   string Query = "select Foreman,count(CommentOne) from database.scaffoldqcdata where Location='" + cb_location + "' Group by Foreman;";
   MySqlConnection conDataBase = new MySqlConnection(constring);
   MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
   MySqlDataReader myReader;
   try
   {
       conDataBase.Open();
       myReader = cmdDataBase.ExecuteReader();
       while (myReader.Read())
       {
           this.chart1.Series["Series1"].Points.AddXY(myReader.GetString("Foreman"), myReader.GetString("CommentOne"));
       }

   }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }

它与其他查询一起工作,但是当我 运行 这个查询时它显示空白。

您需要使用列别名在查询中定义 CommentOne

select Foreman, count(CommentOne) as CommentOne
from database.scaffoldqcdata
where Location='" + cb_location + "'
Group by Foreman

老实说,我建议不要使用那个名字。相反,像这样:

select Foreman, count(CommentOne) as NumComments
from database.scaffoldqcdata
where Location='" + cb_location + "'
Group by Foreman

并在图表代码中使用此名称:

this.chart1.Series["Series1"].Points.AddXY(myReader.GetString("Foreman"), myReader.GetString("NumComments"));

如果您希望结果按特定顺序排列,您可能还想包含一个 order by。您不应依赖 group by 进行任何特定排序。