?
Solved

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

Posted on 2013-10-29
4
Medium Priority
?
225 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 15

Accepted Solution

by:
DonConsolio earned 2000 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

801 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