使用MySql内部联接在Google饼图上显示数据

时间:2018-10-12 08:44:29

标签: c# mysql asp.net json ajax

我需要在google piechart上显示一些数据,并且我必须使用2个表来实现此目的。表名称是身份验证和代理详细信息,并且这两个表都有相似的列,称为“ agentlogin”。 agentdetails具有代理的所有数据,通过身份验证,我可以获取已注册的代理的数量。我需要从也显示在身份验证中的agentdetails中获取agentlogin的数据,我想按skill2列对它们进行分组。

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
    google.load("visualization", "1", { packages: ["corechart"] });
    google.setOnLoadCallback(drawChart);
    function drawChart() {
        var options = {
            title: 'Registration'
        };
        $.ajax({
            type: "POST",
            url: "adminrep.aspx/GetChartData",
            data: '{}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (r) {
                var data = google.visualization.arrayToDataTable(r.d);
                var chart = new google.visualization.PieChart($("#chart")[0]);
                chart.draw(data, options);
            },
            failure: function (r) {
                alert(r.d);
            },
            error: function (r) {
                alert(r.d);
            }
        });
    }
</script>

public static List<object> GetChartData()
    {
        string query = "SELECT authentication.agentlogin, agentdetails.skill2, COUNT(authentication.agentlogin) TotalRegistration FROM agentdetails ";
        query += " INNER JOIN authentication ON agentdetails.agentlogin = authentication.agentlogin WHERE agentdetails.location = 'PNQ10-Pune' GROUP BY skill2";
        string MyConString = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
        List<object> chartData = new List<object>();
        chartData.Add(new object[]
        {
            "skill2", "TotalRegistration"
        });
        using (MySqlConnection con = new MySqlConnection(MyConString))
        {
            using (MySqlCommand cmd = new MySqlCommand(query))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                con.Open();
                using (MySqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        chartData.Add(new object[]
                        {
                            sdr["skill2"], sdr["TotalRegistration"]
                        });
                    }
                }
                con.Close();
                return chartData;
            }
        }
    }

我该如何完成?预先感谢。

1 个答案:

答案 0 :(得分:2)

如果您想计算是否进行了身份验证,那么,您需要一个 LEFT 联接才能从第一个表中获取所有结果,而不仅仅是两个表中都出现的结果

如果表之间的关系是1到1,那么每个座席将只出现一次,则左联接上将不会出现重复的重新填充,并且重新填充应类似于您已经到达的重新填充。

请注意,总注册数量当然仅是已注册的代理商,而不是所有代理商的注册数量:

SELECT
 ad.skill2,
SUM(IF(  auth.agentlogin IS NULL, 1,0)) AS TotalRegistration
 FROM agentdetails ad
LEFT JOIN authentication auth ON ad.agentlogin = auth.agentlogin
WHERE ad.location = 'PNQ10-Pune'
GROUP BY
ad.skill2

您的原始查询:

SELECT authentication.agentlogin, agentdetails.skill2, COUNT(authentication.agentlogin) TotalRegistration FROM agentdetails
        INNER JOIN authentication ON agentdetails.agentlogin = authentication.agentlogin WHERE agentdetails.location = 'PNQ10-Pune' GROUP BY skill2;

希望有帮助