When I do a COUNT/GROUP BY in a query, then a mysql_num_rows(), they return the same number instead of the telescoped number and full number of rows. Right now I run the query twice, first for the COUNT/GROUP BY to get the grouped rows, then again to get the full row count by leaving off the COUNT/GROUP BY:
$sqlqueryc = "SELECT tier_level3_brand, COUNT(*) AS Number
FROM cover_tiers
WHERE 1
GROUP BY tier_level3_brand
ORDER BY Number DESC
";
$sqlc = mysql_query($sqlqueryc);
// Count number of rows returned by query
$sqlqueryac = "SELECT tier_level3_brand
FROM cover_tiers
WHERE 1
";
$sqlac = mysql_query($sqlqueryac);
$countc = mysql_num_rows($sqlac);
Is there a more efficient way than running a query twice like this?
Maybe you could use a MySQL subquery to combine the two queries? Something like;
$sqlqueryc = "SELECT tier_level3_brand, COUNT(*) AS Number, (SELECT COUNT(*) FROM cover_tiers WHERE 1) AS totalcount
FROM cover_tiers
WHERE 1
GROUP BY tier_level3_brand
ORDER BY Number DESC";
You could then use totalcount for your full row count.