简体   繁体   中英

SELECT COUNT with Group BY only return value of 2

I have 6 records 3 of which has identical School and I want to get the result of counting how many school there are inside my database but it only returns the value of 2

$tblnum1 = "SELECT COUNT(*) AS ttldata FROM engoralgrade3 WHERE Years = '$yrr' GROUP BY School";
$tblnum = mysqli_query($conn, $tblnum1);
$tblnm = mysqli_fetch_array($tblnum);

echo $tblnm['ttldata'];//input should be 3

This what my data base looked like

I have checked your table, every school do have 2 rows. maybe u want to count how many distinct school there are, so change the sql to:

select count(distinct School )from engoralgrade3

or u want to distinct the school name, try:

select distinct School from engoralgrade3

You can try this query it will work

$tblnum1 = "SELECT * FROM engoralgrade3 WHERE Years = '$yrr' GROUP BY School";
$tblnum = mysqli_query($conn, $tblnum1);
$tblnm = mysqli_num_rows($tblnum);

echo $tblnm ;

it may be the var $yrr is not identical for all six records in database which cause make returnred value is 2 not 3.

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