RE: [PHP-DB] Question about COUNT(*)
| From: | Erick Wellem | Date: | Fri, 08 Jul 2005 04:07:37 +0000 |
| Subject: | RE: [PHP-DB] Question about COUNT(*) | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-39913@lists.php.net to get a copy of this message | ||
Thanks Rory,
This query solves the problem:
SELECT client.client_name, IFNULL( COUNT( sales.sale_id ) , 0 ) AS total
FROM CLIENT LEFT JOIN sales ON client.client_id = sales.client_id
GROUP BY client.client_name
ORDER BY total DESC
Also thanks to Nandar and Prabhu, but MySQL does not recognize '*='
operator.
You guys is very helpful, thanks again :)
Erick Wellem
<snip>
You could also try something like this:
SELECT client.name, IFNULL(COUNT(sales.sale_id), 0) AS total
FROM client LEFT JOIN sales ON client.client_id = sales.client_id
GROUP BY client.name
ORDER BY total DESC
The left join forces every record in the left table of the join (client)
to be present in the result set whether or not there are matching
entries in the right table (sales). The IFNULL takes care fo your count
problem.
</snip>