Count column of table group by rows

时间:2016-02-12 21:10:30

标签: php

I can count number of a table rows by using mysqli_num_rows. I have a table which contains similar rows. How can I count by grouping by similar row? For instance: I have a table with 2 columns : Student and Option. Let say there are 50 students. 20 are in Economy option, 20 are in Management option and 10 are in Secretary Option. How can display those numbers. I can display only the 50.

my codes

false

2 个答案:

答案 0 :(得分:1)

Here is the query you need:

$(document).ready(function(){
$('.mapa').qtip({
         show: 'click',
         hide: 'click',
         content:{
                    text: function(event, api) {
                             $.ajax({
                                   url: '/google_maps/mapa.php'
                                   })
                            .then(function(content) {
                               api.elements.tooltip.html(content);
                            }, function(xhr, status, error) {
                               api.set('content.text', status + ':  ' + error);
                            });
                            return 'Loading...'; // Set some initial text

                    },
                },
        position: {
        my: 'top right',
        at: 'top center',
        },
        style: {
        classes: 'qtip-blue',
        }
    });
});

Now you can echo the count, along with other data:

SELECT COUNT(*) AS `option_count`, * -- returns a count aliased as "options_count"
FROM `table`
GROUP BY `option` -- will group the options and show the counts

The problem that you have here is that you will not be able to display each student in the option because this counts / groups only the three options.

答案 1 :(得分:0)

看到正确的查询:

$qry = "select option as opt, COUNT(*) AS option from table group by option";