[英]How to fetch 5 most recent values and number of count in a column SQL
According to the table, I have to count the top 3 most frequent bookID and the count of the bookID.根据表格,我必须计算前 3 个最频繁的 bookID 和 bookID 的计数。
I have figured out how to get the top 3, however I couldn't figured how to display the number of count in the bracket after the comma.我已经想出了如何获得前 3 名,但是我不知道如何在逗号后的括号中显示计数的数量。
I have to put these into the brackets separate by commas, it look like this:我必须将它们放在括号中,用逗号分隔,如下所示:
[top1 bookid , the number of count] => [17,6]
[top2 bookid , the number of count] => [13,6]
[top3 bookid , the number of count] => [16,5]
$query ="SELECT bookID FROM issue_books GROUP BY bookID ORDER BY COUNT(*) DESC LIMIT 3 ";
$query_run = mysqli_query($connection,$query);
foreach ($query_run as $row)
{
echo '['". $row['bookID'];.",".xxxxx."']';
}
Is there any way to display the count of the frequency number together?有没有办法一起显示频率数的计数?
Add the count to the select clause?将计数添加到 select 子句?
$query = "SELECT bookID, COUNT(*) AS cnt FROM issue_books GROUP BY bookID ORDER BY cnt DESC LIMIT 3 ";
$query_run = mysqli_query($connection, $query);
foreach ($query_run as $row) {
echo "[" . $row['bookID'] . ", " . $row['cnt'] . "]\n";
}
You could add the count(*)
to the select list:您可以将
count(*)
添加到 select 列表中:
$query =
"SELECT bookID, COUNT(*) AS cnt FROM issue_books GROUP BY bookID ORDER BY COUNT(*) DESC LIMIT 3 ";
$query_run = mysqli_query($connection,$query);
foreach ($query_run as $row)
{
echo '[' . $row['bookID']; . ',' . $row['cnt'] . ']';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.