我已经在c#中搜索了sql的count函数。我正在尝试使用图表来计算每个行标签的条目,并在条形图上显示计数。例如:
-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); }
它可以与其他查询一起使用,但在运行此查询时显示为空白。
答案 0 :(得分:0)
您需要使用列别名
在查询中定义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
来做任何特定的排序。