Jump to content

[SOLVED] Joins


padams

Recommended Posts

I'm running a query that combines results from three tables, including counting and grouping data. It has player details, tries scored and games played. Currently it returns the player names even if they have zero games played. Is it possible to modify the query so that if a player has zero games they do not appear in the results?

 

I'm using a left join, but I know that includes results even if there is nothing in one of the columns. Can I get around the problem by using a different join type? I've tried reading up on them and played around with some but can't get the result I want.

 

SELECT p.`playerID`, p.`playerFirstName`, p.`playerLastName`, IFNULL(g.gamescount,0) as gamescount, IFNULL(t.trycount,0) as trycount FROM players p LEFT JOIN (SELECT playerID, COUNT(*) as gamescount FROM games GROUP BY playerID) as g USING (playerID) LEFT JOIN (SELECT playerID, COUNT(*) as trycount FROM tries GROUP BY playerID) as t  USING (playerID) ORDER BY gamescount DESC

Link to comment
Share on other sites

I don't think you need a join for that. Try this query:

 

SELECT p.playerID, p.playerFirstName, p.playerLastName,
       (SELECT COUNT(g.*) FROM games g WHERE g.playerID = p.playerID) gamescount,
       (SELECT COUNT(t.*) FROM tries t WHERE t.playerID = p.playerID) trycount
  FROM players p
  WHERE gamescount > 0
  ORDER BY gamescount DESC;

Link to comment
Share on other sites

Tried that and got the following error:

 

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*) FROM games g WHERE g.playerID = p.playerID) gamescount,

      (SELECT COUNT' at line 2

Link to comment
Share on other sites

Tried:

SELECT p.playerID, p.playerFirstName, p.playerLastName,

      (SELECT COUNT(*) FROM games g WHERE g.playerID = p.playerID) gamescount,

      (SELECT COUNT(*) FROM tries t WHERE t.playerID = p.playerID) trycount

  FROM players p

  WHERE gamescount > 0

  ORDER BY gamescount DESC;

 

Got another error:  #1054 - Unknown column 'gamescount' in 'where clause'

Link to comment
Share on other sites

Result was another error:

 

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* ) FROM games g WHERE g . playerID = p . playerID ) as gamescount , ( SELECT CO' at line 1

Link to comment
Share on other sites

Tried:

SELECT p.playerID, p.playerFirstName, p.playerLastName,

      (SELECT COUNT(*) FROM games g WHERE g.playerID = p.playerID) gamescount,

      (SELECT COUNT(*) FROM tries t WHERE t.playerID = p.playerID) trycount

  FROM players p

  WHERE gamescount > 0

  ORDER BY gamescount DESC;

 

Got another error:  #1054 - Unknown column 'gamescount' in 'where clause'

 

You can't use an alias in a WHERE clause since the alias is unknown when the table data is being filtered.

 

If you do an INNER JOIN withe games table you will only get those players who have at least one game

Link to comment
Share on other sites

Great, the inner join worked a treat. Thanks for the help.

 

SELECT p.`playerID`, p.`playerFirstName`, p.`playerLastName`, IFNULL(g.gamescount,0) as gamescount, IFNULL(t.trycount,0) as trycount FROM players p INNER JOIN (SELECT playerID, COUNT(*) as gamescount FROM games GROUP BY playerID) as g USING (playerID) LEFT JOIN (SELECT playerID, COUNT(*) as trycount FROM tries GROUP BY playerID) as t  USING (playerID) ORDER BY gamescount DESC

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.