List:General Discussion« Previous MessageNext Message »
From:Baron Schwartz Date:June 8 2007 1:09pm
Subject:Re: Select and COUNT
View as plain text  
Hi spacemarc,

spacemarc wrote:
> Hi all,
> I have this query:
> 
> SELECT tableA.*, COUNT(*) AS Tot
> FROM tableB
> LEFT JOIN tableA ON tableA.uid=tableB.uid
> GROUP BY tableA.uid
> 
> This query shows only the users (tableA) that are in tableB with at
> least 1 record (like total) but not the users that have 0 record.
> 
> How can I obtain all users, also those with 0 record??

Use a LEFT OUTER JOIN, but reverse it; join from tableA to tableB.

You may be looking for a FULL OUTER JOIN, I'm not sure.  MySQL doesn't support that, 
but see: http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/

Baron
Thread
Select and COUNTspacemarc8 Jun
  • Re: Select and COUNTBaron Schwartz8 Jun
  • RE: Select and COUNTJerry Schwartz8 Jun
Re: Select and COUNTBaron Schwartz8 Jun