在select count(*)语句中获取关联列值

时间:2016-09-14 14:25:42

标签: php mysql

请我尝试在选择计数(*)上获取关联列值。这是我的代码。

$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返回我的预期值,而AVERAGE列未定义,$totalscore = $r4['AVERAGE']; AVERAGE是我数据库中的一列。如果我的sql语句是count(*)??这是否意味着我无法获取关联列值?任何想法?

1 个答案:

答案 0 :(得分:0)

您没有在查询中选择average列 您可以选择更多列,如下所示:

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