dumebi dumebi - 2 months ago 7
MySQL Question

fetch associative column value in select count(*) statement

Please i am trying to fetch the associative column value on a select count(*). here is my code.

$res = mysql_query("select count(*) as total from table where reg = '12' GROUP BY YEAR DESC LIMIT 7");
while($r4 = mysql_fetch_assoc($res)){

$totalscore = $r4['AVERAGE'];
$len = $r4['total'];
}


$len
returns my expected value, while the column AVERAGE is undefined i.e
$totalscore = $r4['AVERAGE'];
AVERAGE is a column on my database. Does it mean i cannot fetch associative column value if my sql statement is count(*)?? any ideas?

Answer

You did not select the average column in your query you can select more columns like so:

SELECT  
   COUNT(*) as total,
   `AVERAGE`
   FROM table 
   WHERE reg = '12' 
   GROUP BY YEAR DESC 
   LIMIT 7