简体   繁体   中英

Using MySql inner join to show data on google piechart

I need to show some data on google piechart and I have to use 2 tables to achieve this. The table names being authentication and agentdetails and both of these tables have similar column named "agentlogin". agentdetails have all the datas of agents and from authentication I can get the number of agent who have registered. I need to get the data of agentlogin from agentdetails that also present on authentication and i want to group them by skill2 column.

<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;
            }
        }
    }

Table

How do I get this done? Thanks in advance.

If you would like to count if an authentication had occurred, well, you would need a LEFT join to get all of the results from the 1st table, not just the ones appearing in both

If the relationship between the tables is 1 to 1, so every agent will just appear once, then no duplicate resilts will appear on the left join, and the resilt should be similar to what you have already arrived to

Notice that the total registration count will be only of the registered, not of all agents, of course:

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

Your original query:

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;

Hope that helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM