简体   繁体   中英

Total of Male and Female in Grade 8 using sql, codeigniter

I have a problem about total in Yearlevel like total number of male and female in grade 7: 在此处输入图片说明

I can count all by using codeigniter:

  <h3> Total Students: <?php echo $this->db->count_all('studentinformation'); ?></h3> 

And then group them by using this:

SELECT Yearlevel, Sex, COUNT(Id) AS NumGender
FROM studentinformation
GROUP BY Sex, Yearlevel

My problem now is count in yearlevel total of grade7, grade 8 and then arrage them

Perhaps this is what you're looking for?

Use case expressions to do conditional counting:

SELECT Yearlevel,
       COUNT(*) AS studentcount,
       COUNT(case when Sex = 'Female' then 1 end) AS femalecount,
       COUNT(case when Sex = 'Male' then 1 end)   AS malecount
FROM studentinformation
GROUP BY Yearlevel

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