0
I’m making a report and I need to count and display the number plus the percentage for the total count. With this code I already count. It is possible to display the percentage beside using sql and php?
$sql = "SELECT
customer.country,
COUNT(*) AS count
FROM
customer
INNER JOIN
booking ON booking.customer_id = customer.id
WHERE
(checkin >= '$datainicialsql' AND checkout <= '$datafinalsql')
GROUP BY country";
$query = mysql_query($sql);
$w = 0;
while ($result = mysql_fetch_assoc($query))
{
echo $result['country'] . " - " . $result['count'] . "<br/>";
}
I wanted to make an output like this:
BR 20 (20%)
IT 40 (40%)
PT 40 (40%)
Although it’s possible to do it in one query I do not think it will be very efficient. I suggest, alternatively, that you consult first to get the total records. Then use this value, together with the result of the second query, to calculate the percentage.
– ramaral