RE: Question about COUNT(*)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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>

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux