Link to home
Start Free TrialLog in
Avatar of rjorge
rjorge

asked on

How to retrieve query results when count() = 0 ?

Greetings,
I have the following (similar) structure :

create table players (
  ownerid int,
   name varchar(10)
)

create table games (
  gameid int,
  name varchar(255)
)

create table gameowner (
  ownerid int,
  gameid int
)

I want to make a query where I can list all the owners with less than 'x' games, including those that have none. I am having some problems with the second part. would greatly appreciate your help.

r
Avatar of Barry62
Barry62
Flag of United States of America image

select players.name,count(gameowner.gameid) as games
from players
join gameowner on players.ownerid = gameowner.ownerid
where games < 5
group by players.name,games
Avatar of rjorge
rjorge

ASKER

Hi Barry,
I wonder whether there might be a problem when the result of the join is a null line, because there is no line matching "join gameowner on players.ownerid = gameowner.ownerid", (e.g. there is an id on players for which there is no entry on gameowner table). As I see it may result on on a empty/null set. I still want to see those entries. See example output below.

name | games
john     1
jack      0
sam     2
ASKER CERTIFIED SOLUTION
Avatar of DonConsolio
DonConsolio
Flag of Austria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Don is correct.  Use a left (or outer) join.