jhunlio
jhunlio

Reputation: 2660

count how many group in a column

using count() and group by I can generate a count belong with that group.

example:

DB

 student_number            sem              SY
    2015-0002            2nd sem          2014-2015
    2015-0002            2nd sem          2014-2015
    2015-0002            2nd sem          2014-2015
    2015-0002            1st sem          2015-2016
    2015-0002            1st sem          2015-2016
    2015-0002            1st sem          2015-2016
    2015-0002            2nd sem          2015-2016
    2015-0002            2nd sem          2015-2016
    2015-0002            2nd sem          2015-2016
    2015-0002            2nd sem          2015-2016

query

  $sql  = "SELECT *, count(SY) as student_count ";
  $sql .= "FROM studeaccount ";
  $sql .= "WHERE StudentNumber = '2015-0002' ";
  $sql .= "GROUP BY SY, Sem ";
  $sql .= "ORDER BY StudentNumber ASC";

query above output like this:

2nd sem : 2014-2015 = 3
1st sem : 2015-2016 = 3
2nd sem : 2015-2016 = 4

result above show number of group and that what i want to get, the total number of group. example below i want to output.

Group count = 3 // this is what i want to achieve.

hope this help.

Upvotes: 0

Views: 80

Answers (2)

vinz
vinz

Reputation: 566

SELECT COUNT(*) FROM
(
    SELECT *, count(SY) as student_count
    FROM studeaccount
    WHERE StudentNumber = '2015-0002'
    GROUP BY SY, Sem
    ORDER BY StudentNumber ASC
 ) t1

Upvotes: 1

user4246994
user4246994

Reputation:

try this

$sql = "select count(distinct sem) from student_account";

Upvotes: 0

Related Questions