使用mysql内部连接在google piechart上显示数据

o3imoua4  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(339)

我需要在googlepiechart上显示一些数据,我必须使用两个表来实现这一点。表名是authentication和agentdetails,这两个表都有类似的列,名为“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;
            }
        }
    }


我该怎么做?提前谢谢。

xuo3flqw

xuo3flqw1#

如果您想计算是否发生了身份验证,那么您需要一个左联接来获取第一个表中的所有结果,而不仅仅是出现在两个表中的结果
如果表之间的关系是1比1,那么每个代理只会出现一次,那么左连接上将不会出现重复的resilt,并且resilt应该与您已经到达的类似
请注意,注册总数将仅限于已注册的代理,而不是所有代理,当然:

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;

希望有帮助

相关问题