I have this following SQL statement / PHP recordset;
$sql_result= mysql_query("SELECT * FROM reports GROUP BY post DESC", $db);
while($rs = mysql_fetch_array($sql_result)) {
echo "post $rs[post] has $rs[num] reports<br>";
}
The num field doesn't exist, but I want it to display how many are in that group. I assume it uses a COUNT in the SQL but not sure where.
Any advice?
You use the aggregate COUNT(*)
function along with GROUP BY
to get per-group counts. And DESC
can only be used along with ORDER BY
.
SELECT post, COUNT(*) AS num
FROM reports
GROUP BY post
ORDER BY num DESC
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.