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

发布于 2024-12-21 01:55:42 字数 664 浏览 0 评论 0原文

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

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

但是我想要的是它显示最流行的类别。例如 - 最流行的类别是“”和“”条目

代码:

- $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']    ." ;

}

我将如何解决这个问题?

i have got to the stage where i can display a count of how many entries are in each category. It echo's the data like this

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

However all i want is it to show the most popular category. So for instance - the most popular categoryNo is "" with "" entries

Code:

- $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']    ." ;

}

How would i go about this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一身骄傲 2024-12-28 01:55:42

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

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

Define "the most popular". If you mean the most popular category is a single category containing the most entries, then this would be your query:

SELECT categoryNo, COUNT(entryNo) as numEntries
FROM entries
GROUP by categoryNo
ORDER by numEntries DESC
LIMIT 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文