Solved

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

Posted on 2013-10-29
4
214 Views
Last Modified: 2013-10-30
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
0
Comment
Question by:rjorge
  • 2
4 Comments
 
LVL 8

Expert Comment

by:Barry62
ID: 39610638
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
0
 

Author Comment

by:rjorge
ID: 39610661
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
0
 
LVL 14

Accepted Solution

by:
DonConsolio earned 500 total points
ID: 39610694
use a  left join on players and gameowner to find all from players and matching number of games.

use a subquery to restrict the result to  owners with less than 'x' (2 in this example)  games.

 select * from ( select players.ownerid , name, count(gameid) as numgames from players left join gameowner on players.ownerid  = gameowner.ownerid group by  players.ownerid ) as tsub where numgames < 2;

Open in new window

0
 
LVL 8

Expert Comment

by:Barry62
ID: 39611465
Don is correct.  Use a left (or outer) join.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Creating and Managing Databases with phpMyAdmin in cPanel.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now