选择数据库中条目最多的类别

时间:2011-12-14 23:37:02

标签: php mysql count

我已经到了可以显示每个类别中有多少条目的计数的阶段。它回应了像这样的数据

There are 6 in the category number1
There are 4 in the category number2
There are 1 in the category number4
There are 1 in the category number5
There are 1 in the category number8

然而,我想要的只是展示最受欢迎的类别。例如 - 最受欢迎的categoryNo是“”和“”条目

代码:

- $count_query_v1 = "SELECT categoryNo, COUNT(entryNo)
FROM entries
GROUP by categoryNo;
";

$answer = mysql_query($count_query_v1) or die(mysql_error());

// Print out result
while($row = mysql_fetch_array($answer)){
echo "There are ". $row['COUNT(entryNo)'] ."in the category number". $row['categoryNo']    ." ;

}

我该怎么做?

1 个答案:

答案 0 :(得分:1)

定义"最受欢迎的"。如果您的意思是最受欢迎的类别是包含最多条目的单个类别,那么这将是您的查询:

SELECT categoryNo, COUNT(entryNo) as numEntries
FROM entries
GROUP by categoryNo
ORDER by numEntries DESC
LIMIT 1